home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / zsh-3.0-p / zsh-3 / zsh-3.0-pre3 / Doc / intro.ms < prev    next >
Text File  |  1996-07-01  |  86KB  |  2,702 lines

  1. .nr PI 0
  2. .nr LL 6.5i
  3. .de Ds
  4. .DS I .5i
  5. .ft C
  6. .ps 9
  7. .vs 11
  8. .ss 11
  9. ..
  10. .de De
  11. .DE
  12. .ft R
  13. .ps
  14. .vs
  15. .ss
  16. ..
  17. .de Sh
  18. .SH
  19. \\$1
  20. .XS
  21. \\$1
  22. .XE
  23. ..
  24. .nr HM 4i
  25. .ce 99
  26. .ps 18
  27. .vs 20
  28. .ss 20
  29. \f3An Introduction to the Z Shell\fP
  30.  
  31. .ps 14
  32. .vs 16
  33. .ss 16
  34. \f2Paul Falstad
  35. pf@software.com
  36.  
  37. Bas de Bakker
  38. bas@phys.uva.nl\fP
  39. .ce 0
  40. .nr HM 1i
  41. .pn 1
  42. .bp
  43. .\" This blank page on the reverse of the cover.
  44. .sv |1i
  45. .pn 1
  46. .bp
  47. .TL
  48. An Introduction to the Z Shell
  49. .AU
  50. Paul Falstad
  51. pf@software.com
  52. .AU
  53. Bas de Bakker
  54. bas@phys.uva.nl
  55. .PP
  56. .Sh "Introduction"
  57. .PP
  58. \fBzsh\fP is a shell designed for interactive use, although it is also
  59. a powerful scripting language.  Many of the useful features of bash,
  60. ksh, and tcsh were incorporated into \fBzsh\fP; many original features were
  61. added.  This document details some of the unique features of \fBzsh\fP.  It
  62. assumes basic knowledge of the standard UNIX shells; the intent is to
  63. show a reader already familiar with one of the other major shells what
  64. makes \fBzsh\fP more useful or more powerful.  This document is not at all
  65. comprehensive; read the manual entry for a description of the shell
  66. that is complete and concise, although somewhat overwhelming and
  67. devoid of examples.
  68. .PP
  69. The text will frequently mention options that you can set to change
  70. the behaviour of \fBzsh\fP.  You can set these options with the
  71. command
  72. .Ds
  73. %\0setopt\0\fIoptionname\fC
  74. .De
  75. and unset them again with
  76. .Ds
  77. %\0unsetopt\0\fIoptionname\fC
  78. .De
  79. Case is ignored in option names, as are embedded underscores.
  80. .Sh "Filename Generation"
  81. .PP
  82. Otherwise known as \fIglobbing\fP, filename generation
  83. is quite extensive in \fBzsh\fP.  Of course, it has all the
  84. basics:
  85. .Ds
  86. %\0ls
  87. Makefile\0\0\0file.pro\0\0\0foo.o\0\0\0\0\0\0main.o\0\0\0\0\0q.c\0\0\0\0\0\0\0\0run234\0\0\0\0\0stuff
  88. bar.o\0\0\0\0\0\0foo\0\0\0\0\0\0\0\0link\0\0\0\0\0\0\0morestuff\0\0run123\0\0\0\0\0run240\0\0\0\0\0sub
  89. file.h\0\0\0\0\0foo.c\0\0\0\0\0\0main.h\0\0\0\0\0pipe\0\0\0\0\0\0\0run2\0\0\0\0\0\0\0run303
  90. %\0ls\0*.c
  91. foo.c\0\0q.c
  92. %\0ls\0*.[co]
  93. bar.o\0\0\0foo.c\0\0\0foo.o\0\0\0main.o\0\0q.c
  94. %\0ls\0foo.?
  95. foo.c\0\0foo.o
  96. %\0ls\0*.[^c]
  97. bar.o\0\0\0file.h\0\0foo.o\0\0\0main.h\0\0main.o
  98. %\0ls\0*.[^oh]
  99. foo.c\0\0q.c
  100. .De
  101. Also, if the \fIEXTENDEDGLOB\fP option is set,
  102. some new features are activated.
  103. For example, the \fC^\fP character negates the pattern following it:
  104. .Ds
  105. %\0setopt\0extendedglob
  106. %\0ls\0-d\0^*.c
  107. Makefile\0\0\0file.pro\0\0\0link\0\0\0\0\0\0\0morestuff\0\0run2\0\0\0\0\0\0\0run303
  108. bar.o\0\0\0\0\0\0foo\0\0\0\0\0\0\0\0main.h\0\0\0\0\0pipe\0\0\0\0\0\0\0run234\0\0\0\0\0stuff
  109. file.h\0\0\0\0\0foo.o\0\0\0\0\0\0main.o\0\0\0\0\0run123\0\0\0\0\0run240\0\0\0\0\0sub
  110. %\0ls\0-d\0^*.*
  111. Makefile\0\0\0link\0\0\0\0\0\0\0pipe\0\0\0\0\0\0\0run2\0\0\0\0\0\0\0run240\0\0\0\0\0stuff
  112. foo\0\0\0\0\0\0\0\0morestuff\0\0run123\0\0\0\0\0run234\0\0\0\0\0run303\0\0\0\0\0sub
  113. %\0ls\0-d\0^Makefile
  114. bar.o\0\0\0\0\0\0foo\0\0\0\0\0\0\0\0link\0\0\0\0\0\0\0morestuff\0\0run123\0\0\0\0\0run240\0\0\0\0\0sub
  115. file.h\0\0\0\0\0foo.c\0\0\0\0\0\0main.h\0\0\0\0\0pipe\0\0\0\0\0\0\0run2\0\0\0\0\0\0\0run303
  116. file.pro\0\0\0foo.o\0\0\0\0\0\0main.o\0\0\0\0\0q.c\0\0\0\0\0\0\0\0run234\0\0\0\0\0stuff
  117. %\0ls\0-d\0*.^c
  118. \&.rhosts\0\0\0bar.o\0\0\0\0\0file.h\0\0\0\0file.pro\0\0foo.o\0\0\0\0\0main.h\0\0\0\0main.o
  119. .De
  120. An expression of the form
  121. \fC<\fIx\fR\-\fIy\fC>\fR
  122. matches a range of integers:
  123. .Ds
  124. %\0ls\0run<200-300>
  125. run234\0\0run240
  126. %\0ls\0run<300-400>
  127. run303
  128. %\0ls\0run<-200>
  129. run123\0\0run2
  130. %\0ls\0run<300->
  131. run303
  132. %\0ls\0run<>
  133. run123\0\0run2\0\0\0\0run234\0\0run240\0\0run303
  134. .De
  135. The \fINUMERICGLOBSORT\fP option will sort files with numbers
  136. according to the number.  This will not work with \fCls\fP as it
  137. resorts its arguments:
  138. .Ds
  139. %\0setopt\0numericglobsort
  140. %\0echo\0run<>
  141. run2\0run123\0run234\0run240\0run303
  142. .De
  143. Grouping is possible:
  144. .Ds
  145. %\0ls\0(foo|bar).*
  146. bar.o\0\0foo.c\0\0foo.o
  147. %\0ls\0*.(c|o|pro)
  148. bar.o\0\0\0\0\0file.pro\0\0foo.c\0\0\0\0\0foo.o\0\0\0\0\0main.o\0\0\0\0q.c
  149. .De
  150. Also, the string \fC**/\fP forces a recursive search of
  151. subdirectories:
  152. .Ds
  153. %\0ls\0-R
  154. Makefile\0\0\0file.pro\0\0\0foo.o\0\0\0\0\0\0main.o\0\0\0\0\0q.c\0\0\0\0\0\0\0\0run234\0\0\0\0\0stuff
  155. bar.o\0\0\0\0\0\0foo\0\0\0\0\0\0\0\0link\0\0\0\0\0\0\0morestuff\0\0run123\0\0\0\0\0run240\0\0\0\0\0sub
  156. file.h\0\0\0\0\0foo.c\0\0\0\0\0\0main.h\0\0\0\0\0pipe\0\0\0\0\0\0\0run2\0\0\0\0\0\0\0run303
  157.  
  158. morestuff:
  159.  
  160. stuff:
  161. file\0\0xxx\0\0\0yyy
  162.  
  163. stuff/xxx:
  164. foobar
  165.  
  166. stuff/yyy:
  167. frobar
  168. %\0ls\0**/*bar
  169. stuff/xxx/foobar\0\0stuff/yyy/frobar
  170. %\0ls\0**/f*
  171. file.h\0\0\0\0\0\0\0\0\0\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0foo.o\0\0\0\0\0\0\0\0\0\0\0\0\0stuff/xxx/foobar
  172. file.pro\0\0\0\0\0\0\0\0\0\0foo.c\0\0\0\0\0\0\0\0\0\0\0\0\0stuff/file\0\0\0\0\0\0\0\0stuff/yyy/frobar
  173. %\0ls\0*bar*
  174. bar.o
  175. %\0ls\0**/*bar*
  176. bar.o\0\0\0\0\0\0\0\0\0\0\0\0\0stuff/xxx/foobar\0\0stuff/yyy/frobar
  177. %\0ls\0stuff/**/*bar*
  178. stuff/xxx/foobar\0\0stuff/yyy/frobar
  179. .De
  180. .PP
  181. It is possible to exclude certain files from the patterns using
  182. the ~ character.  A pattern of the form \fC*.c~bar.c\fP lists all
  183. files matching \fC*.c\fP, except for the file \fCbar.c\fP.
  184. .Ds
  185. %\0ls\0*.c
  186. foo.c\0\0\0\0foob.c\0\0\0\0bar.c
  187. %\0ls\0*.c~bar.c
  188. foo.c\0\0\0\0foob.c
  189. %\0ls\0*.c~f*
  190. bar.c
  191. .De
  192. .PP
  193. One can add a number of \fIqualifiers\fP to the end of
  194. any of these patterns, to restrict matches to certain
  195. file types.  A qualified pattern is of the form
  196. .DS
  197. \fIpattern\fC(\fR...\fC)\fR
  198. .De
  199. with single-character qualifiers inside the parentheses.
  200. .Ds
  201. %\0alias\0l='ls\0-dF'
  202. %\0l\0*
  203. Makefile\0\0\0\0foo*\0\0\0\0\0\0\0\0main.h\0\0\0\0\0\0q.c\0\0\0\0\0\0\0\0\0run240
  204. bar.o\0\0\0\0\0\0\0foo.c\0\0\0\0\0\0\0main.o\0\0\0\0\0\0run123\0\0\0\0\0\0run303
  205. file.h\0\0\0\0\0\0foo.o\0\0\0\0\0\0\0morestuff/\0\0run2\0\0\0\0\0\0\0\0stuff/
  206. file.pro\0\0\0\0link@\0\0\0\0\0\0\0pipe\0\0\0\0\0\0\0\0run234\0\0\0\0\0\0sub
  207. %\0l\0*(/)
  208. morestuff/\0\0stuff/
  209. %\0l\0*(@)
  210. link@
  211. %\0l\0*(*)
  212. foo*\0\0\0\0\0\0\0\0link@\0\0\0\0\0\0\0morestuff/\0\0stuff/
  213. %\0l\0*(x)
  214. foo*\0\0\0\0\0\0\0\0link@\0\0\0\0\0\0\0morestuff/\0\0stuff/
  215. %\0l\0*(X)
  216. foo*\0\0\0\0\0\0\0\0link@\0\0\0\0\0\0\0morestuff/\0\0stuff/
  217. %\0l\0*(R)
  218. bar.o\0\0\0\0\0\0\0foo*\0\0\0\0\0\0\0\0link@\0\0\0\0\0\0\0morestuff/\0\0run123\0\0\0\0\0\0run240
  219. file.h\0\0\0\0\0\0foo.c\0\0\0\0\0\0\0main.h\0\0\0\0\0\0pipe\0\0\0\0\0\0\0\0run2\0\0\0\0\0\0\0\0run303
  220. file.pro\0\0\0\0foo.o\0\0\0\0\0\0\0main.o\0\0\0\0\0\0q.c\0\0\0\0\0\0\0\0\0run234\0\0\0\0\0\0stuff/
  221. .De
  222. Note that \fC*(x)\fP and \fC*(*)\fP both match executables.
  223. \fC*(X)\fP matches files executable by others, as opposed to
  224. \fC*(x)\fP, which matches files executable by the owner.
  225. \fC*(R)\fP and \fC*(r)\fP match readable files;
  226. \fC*(W)\fP and \fC*(w)\fP, which checks for writable files.
  227. \fC*(W)\fP is especially important, since it checks for world-writable
  228. files:
  229. .Ds
  230. %\0l\0*(w)
  231. bar.o\0\0\0\0\0\0\0foo*\0\0\0\0\0\0\0\0link@\0\0\0\0\0\0\0morestuff/\0\0run123\0\0\0\0\0\0run240
  232. file.h\0\0\0\0\0\0foo.c\0\0\0\0\0\0\0main.h\0\0\0\0\0\0pipe\0\0\0\0\0\0\0\0run2\0\0\0\0\0\0\0\0run303
  233. file.pro\0\0\0\0foo.o\0\0\0\0\0\0\0main.o\0\0\0\0\0\0q.c\0\0\0\0\0\0\0\0\0run234\0\0\0\0\0\0stuff/
  234. %\0l\0*(W)
  235. link@\0\0\0run240
  236. %\0l\0-l\0link\0run240
  237. lrwxrwxrwx\0\01\0pfalstad\0\0\0\0\0\0\010\0May\023\018:12\0link\0->\0/usr/bin/
  238. -rw-rw-rw-\0\01\0pfalstad\0\0\0\0\0\0\0\00\0May\023\018:12\0run240
  239. .De
  240. If you want to have all the files of a certain type as well as all
  241. symbolic links pointing to files of that type, prefix the qualifier
  242. with a \fC-\fP:
  243. .Ds
  244. %\0l\0*(-/)
  245. link@\0\0\0\0\0\0\0morestuff/\0\0stuff/
  246. .De
  247. You can filter out the symbolic links with the \fC^\fP character:
  248. .Ds
  249. %\0l\0*(W^@)
  250. run240
  251. %\0l\0*(x)
  252. foo*\0\0\0\0\0\0\0\0link@\0\0\0\0\0\0\0morestuff/\0\0stuff/
  253. %\0l\0*(x^@/)
  254. foo*
  255. .De
  256. To find all plain files, you can use \fC.\fP:
  257. .Ds
  258. %\0l\0*(.)
  259. Makefile\0\0file.h\0\0\0\0foo*\0\0\0\0\0\0foo.o\0\0\0\0\0main.o\0\0\0\0run123\0\0\0\0run234\0\0\0\0run303
  260. bar.o\0\0\0\0\0file.pro\0\0foo.c\0\0\0\0\0main.h\0\0\0\0q.c\0\0\0\0\0\0\0run2\0\0\0\0\0\0run240\0\0\0\0sub
  261. %\0l\0*(^.)
  262. link@\0\0\0\0\0\0\0morestuff/\0\0pipe\0\0\0\0\0\0\0\0stuff/
  263. %\0l\0s*(.)
  264. stuff/\0\0\0sub
  265. %\0l\0*(p)
  266. pipe
  267. %\0l\0-l\0*(p)
  268. prw-r--r--\0\01\0pfalstad\0\0\0\0\0\0\0\00\0May\023\018:12\0pipe
  269. .De
  270. \fC*(U)\fP matches all files owned by you.
  271. To search for all files not owned by you, use \fC*(^U)\fP:
  272. .Ds
  273. %\0l\0-l\0*(^U)
  274. -rw-------\0\01\0subbarao\0\0\0\0\0\0\029\0May\023\018:13\0sub
  275. .De
  276. This searches for setuid files:
  277. .Ds
  278. %\0l\0-l\0*(s)
  279. -rwsr-xr-x\0\01\0pfalstad\0\0\0\0\0\0\016\0May\023\018:12\0foo*
  280. .De
  281. This checks for a certain user's files:
  282. .Ds
  283. %\0l\0-l\0*(u[subbarao])
  284. -rw-------\0\01\0subbarao\0\0\0\0\0\0\029\0May\023\018:13\0sub
  285. .De
  286. .Sh "Startup Files"
  287. .PP
  288. There are five startup files that \fBzsh\fP will read commands from:
  289. .Ds
  290. $ZDOTDIR/.zshenv
  291. $ZDOTDIR/.zprofile
  292. $ZDOTDIR/.zshrc
  293. $ZDOTDIR/.zlogin
  294. $ZDOTDIR/.zlogout
  295. .De
  296. If \fBZDOTDIR\fP is not set, then the value of \fBHOME\fP is used;
  297. this is the usual case.
  298. .\".KE    <--- missing .KS or .KF above
  299. .PP
  300. \&\fC.zshenv\fP is sourced on all invocations of the shell,
  301. unless the \fC-f\fP option is set.  It should contain commands to set
  302. the command search path, plus other important environment
  303. variables.
  304. \&\fC.zshenv\fP should not contain commands that produce output
  305. or assume the shell is attached to a tty.
  306. .PP
  307. \&\fC.zshrc\fP is sourced in interactive shells.  It should contain
  308. commands to set up aliases, functions, options, key bindings, etc.
  309. .PP
  310. \&\fC.zlogin\fP is sourced in login shells.  It should contain
  311. commands that should be executed only in login shells.
  312. \&\fC.zlogout\fP is sourced when login shells exit.
  313. \&\fC.zprofile\fP is similar to \fC.zlogin\fP, except that it is sourced before
  314. \&\fC.zshrc\fP.
  315. \&\fC.zprofile\fP is meant as an alternative to \fC.zlogin\fP for
  316. ksh fans;
  317. the two are not intended to be used together, although this
  318. could certainly be done if desired.
  319. \&\fC.zlogin\fP is not the place for alias definitions, options, environment
  320. variable settings, etc.;
  321. as a general rule, it should not change the shell environment
  322. at all.  Rather, it should be used to set the terminal type
  323. and run a series of external commands (\fCfortune\fP, \fCmsgs\fP, etc).
  324. .Sh "Shell Functions"
  325. .PP
  326. \fBzsh\fP also allows you to create your own commands by defining shell
  327. functions.  For example:
  328. .Ds
  329. %\0yp\0()\0{
  330. >\0\0\0\0\0\0\0ypmatch\0$1\0passwd.byname
  331. >\0}
  332. %\0yp\0pfalstad
  333. pfalstad:*:3564:35:Paul\0John\0Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  334. .De
  335. This function looks up a user in the NIS password map.
  336. The \fC$1\fP expands to the first argument to \fCyp\fP.
  337. The function could have been equivalently defined in one of the following
  338. ways:
  339. .Ds
  340. %\0function\0yp\0{
  341. >\0\0\0\0\0\0\0ypmatch\0$1\0passwd.byname
  342. >\0}
  343. %\0function\0yp\0()\0{
  344. >\0\0\0\0\0\0\0ypmatch\0$1\0passwd.byname
  345. >\0}
  346. %\0function\0yp\0()\0ypmatch\0$1\0passwd.byname
  347. .De
  348. Note that aliases are expanded when the function definition is
  349. parsed, not when the function is executed.  For example:
  350. .Ds
  351. %\0alias\0ypmatch=echo
  352. %\0yp\0pfalstad
  353. pfalstad:*:3564:35:Paul\0John\0Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  354. .De
  355. Since the alias was defined after the function was parsed, it has
  356. no effect on the function's execution.
  357. However, if we define the function again with the alias in place:
  358. .Ds
  359. %\0function\0yp\0()\0{\0ypmatch\0$1\0passwd.byname\0}
  360. %\0yp\0pfalstad
  361. pfalstad\0passwd.byname
  362. .De
  363. it is parsed with the new alias definition in place.
  364. Therefore, in general you must define aliases before functions.
  365. .\".KE    <--- missing .KS or .KF above
  366. .PP
  367. We can make the function take multiple arguments:
  368. .Ds
  369. %\0unalias\0ypmatch
  370. %\0yp\0()\0{
  371. >\0\0\0\0\0\0\0for\0i
  372. >\0\0\0\0\0\0\0do\0ypmatch\0$i\0passwd.byname
  373. >\0\0\0\0\0\0\0done
  374. >\0}
  375. %\0yp\0pfalstad\0subbarao\0sukthnkr
  376. pfalstad:*:3564:35:Paul\0John\0Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  377. subbarao:*:3338:35:Kartik\0Subbarao:/u/subbarao:/usr/princeton/bin/zsh
  378. sukthnkr:*:1267:35:Rahul\0Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  379. .De
  380. The \fCfor i\fP loops through each of the function's arguments,
  381. setting \fCi\fP equal to each of them in turn.
  382. We can also make the function do something sensible
  383. if no arguments are given:
  384. .Ds
  385. %\0yp\0()\0{
  386. >\0\0\0\0\0\0\0if\0((\0$#\0==\00\0))
  387. >\0\0\0\0\0\0\0then\0echo\0usage:\0yp\0name\0...;\0fi
  388. >\0\0\0\0\0\0\0for\0i;\0do\0ypmatch\0$i\0passwd.byname;\0done
  389. >\0}
  390. %\0yp
  391. usage:\0yp\0name\0...
  392. %\0yp\0pfalstad\0sukthnkr
  393. pfalstad:*:3564:35:Paul\0John\0Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  394. sukthnkr:*:1267:35:Rahul\0Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  395. .De
  396. \fC$#\fP is the number of arguments supplied to the function.
  397. If it is equal to zero, we print a usage message; otherwise,
  398. we loop through the arguments, and \fCypmatch\fP all of them.
  399. .\".KE    <--- missing .KS or .KF above
  400. .PP
  401. Here's a function that selects a random line from a file:
  402. .Ds
  403. %\0randline\0()\0{
  404. >\0\0\0\0\0\0\0integer\0z=$(wc\0-l\0<$1)
  405. >\0\0\0\0\0\0\0sed\0-n\0$[RANDOM\0%\0z\0+\01]p\0$1
  406. >\0}
  407. %\0randline\0/etc/motd
  408. PHOENIX\0WILL\0BE\0DOWN\0briefly\0Friday\0morning,\05/24/91\0from\08\0AM\0to
  409. %\0randline\0/etc/motd
  410. SunOS\0Release\04.1.1\0(PHOENIX)\0#19:\0Tue\0May\014\019:03:15\0EDT\01991
  411. %\0randline\0/etc/motd
  412. |\0Please\0use\0the\0"msgs"\0command\0to\0read\0announcements.\0\0Refer\0to\0the\0\0\0|
  413. %\0echo\0$z
  414.  
  415. %
  416. .De
  417. \fCrandline\fP has a local variable, \fCz\fP, that holds the number of
  418. lines in the file.  \fC$[RANDOM % z + 1]\fP expands to a random number
  419. between 1 and \fCz\fP.  An expression of the form \fC$[\fR...\fC]\fR
  420. expands to the value of the arithmetic expression within the brackets,
  421. and the \fBRANDOM\fP variable returns a random number each time it
  422. is referenced.  \fC%\fP is the modulus operator, as in C.
  423. Therefore, \fCsed -n $[RANDOM%z+1]p\fP picks a random line from its
  424. input, from 1 to \fCz\fP.
  425. .PP
  426. Function definitions can be viewed with the \fCfunctions\fP builtin:
  427. .Ds
  428. %\0functions\0randline
  429. randline\0()\0{
  430. \0\0\0\0\0\0\0\0integer\0z=$(wc\0-l\0<$1)
  431. \0\0\0\0\0\0\0\0sed\0-n\0$[RANDOM\0%\0z\0+\01]p\0$1
  432.  
  433. }
  434. %\0functions
  435. yp\0()\0{
  436. \0\0\0\0\0\0\0\0if\0let\0$#\0==\00\0
  437. \0\0\0\0\0\0\0\0
  438. \0\0\0\0\0\0\0\0then
  439. \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0echo\0usage:\0yp\0name\0...
  440. \0\0\0\0\0\0\0\0
  441. \0\0\0\0\0\0\0\0fi
  442. \0\0\0\0\0\0\0\0for\0i
  443. \0\0\0\0\0\0\0\0do
  444. \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0ypmatch\0$i\0passwd.byname
  445. \0\0\0\0\0\0\0\0
  446. \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0done
  447.  
  448. }
  449. randline\0()\0{
  450. \0\0\0\0\0\0\0\0integer\0z=$(wc\0-l\0<$1)
  451. \0\0\0\0\0\0\0\0sed\0-n\0$[RANDOM\0%\0z\0+\01]p\0$1
  452.  
  453. }
  454. .De
  455. Here's another one:
  456. .Ds
  457. %\0cx\0()\0{\0chmod\0+x\0$*\0}
  458. %\0ls\0-l\0foo\0bar
  459. -rw-r--r--\0\01\0pfalstad\0\0\0\0\0\0\029\0May\024\004:38\0bar
  460. -rw-r--r--\0\01\0pfalstad\0\0\0\0\0\0\029\0May\024\004:38\0foo
  461. %\0cx\0foo\0bar
  462. %\0ls\0-l\0foo\0bar
  463. -rwxr-xr-x\0\01\0pfalstad\0\0\0\0\0\0\029\0May\024\004:38\0bar
  464. -rwxr-xr-x\0\01\0pfalstad\0\0\0\0\0\0\029\0May\024\004:38\0foo
  465. .De
  466. Note that this could also have been implemented as an alias:
  467. .Ds
  468. %\0chmod\0644\0foo\0bar
  469. %\0alias\0cx='chmod\0+x'
  470. %\0cx\0foo\0bar
  471. %\0ls\0-l\0foo\0bar
  472. -rwxr-xr-x\0\01\0pfalstad\0\0\0\0\0\0\029\0May\024\004:38\0bar
  473. -rwxr-xr-x\0\01\0pfalstad\0\0\0\0\0\0\029\0May\024\004:38\0foo
  474. .De
  475. .PP
  476. Instead of defining a lot of functions in your \fC.zshrc\fP,
  477. all of which you may not use,
  478. it is often better to use the \fCautoload\fP builtin.
  479. The idea is, you create a directory where function
  480. definitions are stored, declare the names in
  481. your \fC.zshrc\fP, and tell the shell where to look for them.
  482. Whenever you reference a function, the shell
  483. will automatically load it into memory.
  484. .Ds
  485. %\0mkdir\0/tmp/funs
  486. %\0cat\0>/tmp/funs/yp
  487. ypmatch\0$1\0passwd.byname
  488. ^D
  489. %\0cat\0>/tmp/funs/cx
  490. chmod\0+x\0$*
  491. ^D
  492. %\0FPATH=/tmp/funs
  493. %\0autoload\0cx\0yp
  494. %\0functions\0cx\0yp
  495. undefined\0cx\0()
  496. undefined\0yp\0()
  497. %\0chmod\0755\0/tmp/funs/{cx,yp}
  498. %\0yp\0egsirer
  499. egsirer:*:3214:35:Emin\0Gun\0Sirer:/u/egsirer:/bin/sh
  500. %\0functions\0yp
  501. yp\0()\0{
  502. \0\0\0\0\0\0\0\0ypmatch\0$1\0passwd.byname
  503. }
  504. .De
  505. This idea has other benefits.  By adding a \fC#!\fP header
  506. to the files, you can make them double as shell scripts.
  507. (Although it is faster to use them as functions, since a
  508. separate process is not created.)
  509. .Ds
  510. %\0ed\0/tmp/funs/yp
  511. 25
  512. i
  513. #!\0/usr/local/bin/zsh
  514. .
  515. w
  516. 42
  517. q
  518. %\0</tmp/funs/yp
  519. #!\0/usr/local/bin/zsh
  520. ypmatch\0$1\0passwd.byname
  521. %\0/tmp/funs/yp\0sukthnkr
  522. sukthnkr:*:1267:35:Rahul\0Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  523. .De
  524. Now other people, who may not use \fBzsh\fP, or who don't want to
  525. copy all of your \fC.zshrc\fP, may use these functions as shell
  526. scripts.
  527. .Sh "Directories"
  528. .PP
  529. One nice feature of \fBzsh\fP is the way it prints directories.
  530. For example, if we set the prompt like this:
  531. .Ds
  532. phoenix%\0PROMPT='%~>\0'
  533. ~>\0cd\0src
  534. ~/src>
  535. .De
  536. the shell will print the current directory in the prompt,
  537. using the \fC~\fP character.
  538. However, \fBzsh\fP is smarter than most other shells in this respect:
  539. .Ds
  540. ~/src>\0cd\0~subbarao
  541. ~subbarao>\0cd\0~maruchck
  542. ~maruchck>\0cd\0lib
  543. ~maruchck/lib>\0cd\0fun
  544. ~maruchck/lib/fun>\0foo=/usr/princeton/common/src
  545. ~maruchck/lib/fun>\0cd\0~foo
  546. ~foo>\0cd\0..
  547. /usr/princeton/common>\0cd\0src
  548. ~foo>\0cd\0news/nntp
  549. ~foo/news/nntp>\0cd\0inews
  550. ~foo/news/nntp/inews>
  551. .De
  552. Note that \fBzsh\fP prints \fIother\fP users' directories 
  553. in the form \fC~user\fP.  Also note that you can
  554. set a parameter and use it as a directory name;
  555. \fBzsh\fP will act as if \fCfoo\fP is a user
  556. with the login directory \fC/usr/princeton/common/src\fP.
  557. This is convenient, especially if you're sick of seeing
  558. prompts like this:
  559. .Ds
  560. phoenix:/usr/princeton/common/src/X.V11R4/contrib/clients/xv/docs>
  561. .De
  562. If you get stuck in this position, you can give the current
  563. directory a short name, like this:
  564. .Ds
  565. /usr/princeton/common/src/news/nntp/inews>\0inews=$PWD
  566. /usr/princeton/common/src/news/nntp/inews>\0echo\0~inews
  567. /usr/princeton/common/src/news/nntp/inews
  568. ~inews>
  569. .De
  570. When you reference a directory in the form \fC~inews\fP,
  571. the shell assumes that you want the directory displayed
  572. in this form; thus simply typing \fCecho ~inews\fP or
  573. \fCcd ~inews\fP causes the prompt to be shortened.
  574. You can define a shell function for this purpose:
  575. .Ds
  576. ~inews>\0namedir\0()\0{\0$1=$PWD\0;\0\0:\0~$1\0}
  577. ~inews>\0cd\0/usr/princeton/bin
  578. /usr/princeton/bin>\0namedir\0pbin
  579. ~pbin>\0cd\0/var/spool/mail
  580. /var/spool/mail>\0namedir\0spool
  581. ~spool>\0cd\0.msgs
  582. ~spool/.msgs>
  583. .De
  584. You may want to add this one-line function to your \fC.zshrc\fP.
  585.  
  586. \fBzsh\fP can also put the current directory in your title bar,
  587. if you are using a windowing system.
  588. One way to do this is with the \fCchpwd\fP function, which is
  589. automatically executed by the shell whenever you change
  590. directory.  If you are using xterm, this will work:
  591. .Ds
  592. chpwd\0()\0{\0print\0-Pn\0'^[]2;%~^G'\0}
  593. .De
  594. The \fC-P\fP option tells \fCprint\fP to treat its arguments like a prompt
  595. string; otherwise the \fC%~\fP would not be expanded.
  596. The \fC-n\fP option suppresses the terminating newline, as with \fCecho\fP.
  597. .PP
  598. If you are using an IRIS \fCwsh\fP, do this:
  599. .Ds
  600. chpwd\0()\0{\0print\0-Pn\0'\e2201.y%~\e234'\0}
  601. .De
  602. The \fCprint -D\fP command has other uses.  For example, to
  603. print the current directory to standard output in short form,
  604. you can do this:
  605. .Ds
  606. %\0print\0-D\0$PWD
  607. ~subbarao/src
  608. .De
  609. and to print each component of the path in short form:
  610. .Ds
  611. %\0print\0-D\0$path
  612. /bin\0/usr/bin\0~locbin\0~locbin/X11\0~/bin
  613. .De
  614. .Sh "Directory Stacks"
  615. .PP
  616. If you use csh, you may know about directory stacks.
  617. The \fCpushd\fP command puts the current directory on the
  618. stack, and changes to a new directory; the \fCpopd\fP command
  619. pops a directory off the stack and changes to it.
  620. .Ds
  621. phoenix%\0cd\0
  622. phoenix%\0PROMPT='Z\0%~>\0'
  623. Z\0~>\0pushd\0/tmp
  624. /tmp\0~
  625. Z\0/tmp>\0pushd\0/usr/etc
  626. /usr/etc\0/tmp\0~
  627. Z\0/usr/etc>\0pushd\0/usr/bin
  628. /usr/bin\0/usr/etc\0/tmp\0~
  629. Z\0/usr/bin>\0popd
  630. /usr/etc\0/tmp\0~
  631. Z\0/usr/etc>\0popd
  632. /tmp\0~
  633. Z\0/tmp>\0pushd\0/etc
  634. /etc\0/tmp\0~
  635. Z\0/etc>\0popd\0
  636. /tmp\0~
  637. .De
  638. \fBzsh\fP's directory stack commands work similarly.  One
  639. difference is the way \fCpushd\fP is handled if no arguments
  640. are given.  As in csh, this exchanges the top two elements
  641. of the directory stack:
  642. .Ds
  643. Z\0/tmp>\0dirs
  644. /tmp\0~
  645. Z\0/tmp>\0pushd
  646. ~\0/tmp
  647. .De
  648. unless the stack only has one entry:
  649. .Ds
  650. Z\0~>\0popd
  651. /tmp
  652. Z\0/tmp>\0dirs
  653. /tmp
  654. Z\0/tmp>\0pushd
  655. ~\0/tmp
  656. Z\0~>
  657. .De
  658. or unless the \fIPUSHDTOHOME\fP option is set:
  659. .Ds
  660. Z\0~>\0setopt\0pushdtohome
  661. Z\0~>\0pushd
  662. ~\0~\0/tmp
  663. .De
  664. .PP
  665. As an alternative to using directory stacks in this manner,
  666. we can get something like a \fIdirectory history\fP
  667. by setting a few more options and parameters:
  668. .Ds
  669. ~>\0DIRSTACKSIZE=8
  670. ~>\0setopt\0autopushd\0pushdminus\0pushdsilent\0pushdtohome
  671. ~>\0alias\0dh='dirs\0-v'
  672. ~>\0cd\0/tmp
  673. /tmp>\0cd\0/usr
  674. /usr>\0cd\0bin
  675. /usr/bin>\0cd\0../pub
  676. /usr/pub>\0dh
  677. 0\0\0\0\0\0\0\0/usr/pub
  678. 1\0\0\0\0\0\0\0/usr/bin
  679. 2\0\0\0\0\0\0\0/usr
  680. 3\0\0\0\0\0\0\0/tmp
  681. 4\0\0\0\0\0\0\0~
  682. /usr/pub>\0cd\0-3
  683. /tmp>\0dh
  684. 0\0\0\0\0\0\0\0/tmp
  685. 1\0\0\0\0\0\0\0/usr/pub
  686. 2\0\0\0\0\0\0\0/usr/bin
  687. 3\0\0\0\0\0\0\0/usr
  688. 4\0\0\0\0\0\0\0~
  689. /tmp>\0ls\0=2/df
  690. /usr/bin/df
  691. /tmp>\0cd\0-4
  692. ~>
  693. .De
  694. Note that \fC=2\fP expanded to the second directory in the
  695. history list, and that \fCcd -3\fP recalled the third
  696. directory in the list.
  697. .PP
  698. You may be wondering what all those options do.
  699. \fIAUTOPUSHD\fP made \fCcd\fP act like \fCpushd\fP.
  700. (\fCalias cd=pushd\fP is not sufficient, for various reasons.)
  701. \fIPUSHDMINUS\fP swapped the meaning of \fCcd +1\fP and
  702. \fCcd -1\fP; we want them to mean the opposite of what they mean in csh,
  703. because it makes more sense in this scheme, and it's easier to type:
  704. .Ds
  705. ~>\0dh
  706. 0\0\0\0\0\0\0\0~
  707. 1\0\0\0\0\0\0\0/tmp
  708. 2\0\0\0\0\0\0\0/usr/pub
  709. 3\0\0\0\0\0\0\0/usr/bin
  710. 4\0\0\0\0\0\0\0/usr
  711. ~>\0unsetopt\0pushdminus
  712. ~>\0cd\0+1
  713. /tmp>\0dh
  714. 0\0\0\0\0\0\0\0/tmp
  715. 1\0\0\0\0\0\0\0~
  716. 2\0\0\0\0\0\0\0/usr/pub
  717. 3\0\0\0\0\0\0\0/usr/bin
  718. 4\0\0\0\0\0\0\0/usr
  719. /tmp>\0cd\0+2
  720. /usr/pub>
  721. .De
  722. \fIPUSHDSILENT\fP keeps the shell from printing
  723. the directory stack each time we do a \fCcd\fP,
  724. and \fIPUSHDTOHOME\fP we mentioned earlier:
  725. .Ds
  726. /usr/pub>\0unsetopt\0pushdsilent
  727. /usr/pub>\0cd\0/etc
  728. /etc\0/usr/pub\0/tmp\0~\0/usr/bin\0/usr
  729. /etc>\0cd
  730. ~\0/etc\0/usr/pub\0/tmp\0~\0/usr/bin\0/usr
  731. ~>\0unsetopt\0pushdtohome
  732. ~>\0cd
  733. /etc\0~\0/usr/pub\0/tmp\0~\0/usr/bin\0/usr
  734. /etc>
  735. .De
  736. \fBDIRSTACKSIZE\fP keeps the directory stack
  737. from getting too large, much like \fIHISTSIZE\fP:
  738. .Ds
  739. /etc>\0setopt\0pushdsilent
  740. /etc>\0cd\0/
  741. />\0cd\0/
  742. />\0cd\0/
  743. />\0cd\0/
  744. />\0cd\0/
  745. />\0cd\0/
  746. />\0cd\0/
  747. />\0cd\0/
  748. />\0dh
  749. 0\0\0\0\0\0\0\0/
  750. 1\0\0\0\0\0\0\0/
  751. 2\0\0\0\0\0\0\0/
  752. 3\0\0\0\0\0\0\0/
  753. 4\0\0\0\0\0\0\0/
  754. 5\0\0\0\0\0\0\0/
  755. 6\0\0\0\0\0\0\0/
  756. 7\0\0\0\0\0\0\0/
  757. .De
  758. .Sh "Command/Process Substitution"
  759. .PP
  760. Command substitution in \fBzsh\fP can take two forms.
  761. In the traditional form, a command enclosed in
  762. backquotes (\fC`\fP...\fC`\fP) is replaced on the command line with its output.
  763. This is the form used by the older shells.
  764. Newer shells (like \fBzsh\fP) also provide another form,
  765. \fC$(\fR...\fC)\fR.  This form is much easier to nest.
  766. .Ds
  767. %\0ls\0-l\0`echo\0/vmunix`
  768. -rwxr-xr-x\0\01\0root\0\0\0\0\0\01209702\0May\014\019:04\0/vmunix
  769. %\0ls\0-l\0$(echo\0/vmunix)
  770. -rwxr-xr-x\0\01\0root\0\0\0\0\0\01209702\0May\014\019:04\0/vmunix
  771. %\0who\0|\0grep\0mad
  772. subbarao\0ttyt7\0\0\0May\023\015:02\0\0\0(mad55sx15.Prince)
  773. pfalstad\0ttyu1\0\0\0May\023\016:25\0\0\0(mad55sx14.Prince)
  774. subbarao\0ttyu6\0\0\0May\023\015:04\0\0\0(mad55sx15.Prince)
  775. pfalstad\0ttyv3\0\0\0May\023\016:25\0\0\0(mad55sx14.Prince)
  776. %\0who\0|\0grep\0mad\0|\0awk\0'{print\0$2}'
  777. ttyt7
  778. ttyu1
  779. ttyu6
  780. ttyv3
  781. %\0cd\0/dev;\0ls\0-l\0$(who\0|
  782. >\0grep\0$(echo\0mad)\0|
  783. >\0awk\0'{\0print\0$2\0}')
  784. crwx-w----\0\01\0subbarao\0\020,\0\071\0May\023\018:35\0ttyt7
  785. crw--w----\0\01\0pfalstad\0\020,\0\081\0May\023\018:42\0ttyu1
  786. crwx-w----\0\01\0subbarao\0\020,\0\086\0May\023\018:38\0ttyu6
  787. crw--w----\0\01\0pfalstad\0\020,\0\099\0May\023\018:41\0ttyv3
  788. .De
  789. Many common uses of command substitution, however, are
  790. superseded by other mechanisms of \fBzsh\fP:
  791. .Ds
  792. %\0ls\0-l\0`tty`
  793. crw-rw-rw-\0\01\0root\0\0\0\0\0\020,\0\028\0May\023\018:35\0/dev/ttyqc
  794. %\0ls\0-l\0$TTY
  795. crw-rw-rw-\0\01\0root\0\0\0\0\0\020,\0\028\0May\023\018:35\0/dev/ttyqc
  796. %\0ls\0-l\0`which\0rn`
  797. -rwxr-xr-x\0\01\0root\0\0\0\0\0\0\0172032\0Mar\0\06\018:40\0/usr/princeton/bin/rn
  798. %\0ls\0-l\0=rn
  799. -rwxr-xr-x\0\01\0root\0\0\0\0\0\0\0172032\0Mar\0\06\018:40\0/usr/princeton/bin/rn
  800. .De
  801. A command name with a \fC=\fP prepended is replaced with its full
  802. pathname.  This can be very convenient.  If it's not convenient
  803. for you, you can turn it off:
  804. .Ds
  805. %\0ls
  806. =foo\0\0\0\0=bar
  807. %\0ls\0=foo\0=bar
  808. zsh:\0foo\0not\0found
  809. %\0setopt\0noequals
  810. %\0ls\0=foo\0=bar
  811. =foo\0\0\0\0=bar
  812. .De
  813. .PP
  814. Another nice feature is process substitution:
  815. .Ds
  816. %\0who\0|\0fgrep\0-f\0=(print\0-l\0root\0lemke\0shgchan\0subbarao)
  817. root\0\0\0\0\0console\0May\019\010:41
  818. lemke\0\0\0\0ttyq0\0\0\0May\022\010:05\0\0\0(narnia:0.0)
  819. lemke\0\0\0\0ttyr7\0\0\0May\022\010:05\0\0\0(narnia:0.0)
  820. lemke\0\0\0\0ttyrd\0\0\0May\022\010:05\0\0\0(narnia:0.0)
  821. shgchan\0\0ttys1\0\0\0May\023\016:52\0\0\0(gaudi.Princeton.)
  822. subbarao\0ttyt7\0\0\0May\023\015:02\0\0\0(mad55sx15.Prince)
  823. subbarao\0ttyu6\0\0\0May\023\015:04\0\0\0(mad55sx15.Prince)
  824. shgchan\0\0ttyvb\0\0\0May\023\016:51\0\0\0(gaudi.Princeton.)
  825. .De
  826. A command of the form \fC=(\fR...\fC)\fR is replaced with the name of a \fIfile\fP
  827. containing its output.  (A command substitution, on the other
  828. hand, is replaced with the output itself.)
  829. \fCprint -l\fP is like \fCecho\fP, excepts that it prints its arguments
  830. one per line, the way \fCfgrep\fP expects them:
  831. .Ds
  832. %\0print\0-l\0foo\0bar
  833. foo
  834. bar
  835. .De
  836. We could also have written:
  837. .Ds
  838. %\0who\0|\0fgrep\0-f\0=(echo\0'root
  839. >\0lemke
  840. >\0shgchan
  841. >\0subbarao')
  842. .De
  843. Using\0process\0substitution,
  844. you\0can\0edit\0the\0output\0of\0a\0command:
  845. .Ds
  846. %\0ed\0=(who\0|\0fgrep\0-f\0~/.friends)
  847. 355
  848. g/lemke/d
  849. w\0/tmp/filbar
  850. 226
  851. q
  852. %\0cat\0/tmp/filbar
  853. root\0\0\0\0\0console\0May\019\010:41
  854. shgchan\0\0ttys1\0\0\0May\023\016:52\0\0\0(gaudi.Princeton.)
  855. subbarao\0ttyt7\0\0\0May\023\015:02\0\0\0(mad55sx15.Prince)
  856. subbarao\0ttyu6\0\0\0May\023\015:04\0\0\0(mad55sx15.Prince)
  857. shgchan\0\0ttyvb\0\0\0May\023\016:51\0\0\0(gaudi.Princeton.)
  858. .De
  859. or easily read archived mail:
  860. .Ds
  861. %\0mail\0-f\0=(zcat\0~/mail/oldzshmail.Z)
  862. "/tmp/zsha06024":\084\0messages,\00\0new,\043\0unread
  863. >\0\01\0\0U\0\0TO:\0pfalstad,\0zsh\0(10)
  864. \0\0\02\0\0U\0\0nytim!tim@uunet.uu.net,\0Re:\0Zsh\0on\0Sparc1\0/SunOS\04.0.3
  865. \0\0\03\0\0U\0\0JAM%TPN@utrcgw.utc.com,\0zsh\0fix\0(15)
  866. \0\0\04\0\0U\0\0djm@eng.umd.edu,\0way\0to\0find\0out\0if\0running\0zsh?\0(25)
  867. \0\0\05\0\0U\0\0djm@eng.umd.edu,\0Re:\0way\0to\0find\0out\0if\0running\0zsh?\0(17)
  868. \0\0\06\0\0\0r\0djm@eng.umd.edu,\0Meta\0.\0(18)
  869. \0\0\07\0\0U\0\0jack@cs.glasgow.ac.uk,\0Re:\0problem\0building\0zsh\0(147)
  870. \0\0\08\0\0U\0\0nytim!tim@uunet.uu.net,\0Re:\0Zsh\0on\0Sparc1\0/SunOS\04.0.3
  871. \0\0\09\0\0\0\0\0ursa!jmd,\0Another\0fix...\0(61)
  872. \0\010\0\0U\0\0pplacewa@bbn.com,\0Re:\0v18i084:\0Zsh\02.00\0-\0A\0small\0complaint\0(36)
  873. \0\011\0\0U\0\0lubkin@cs.rochester.edu,\0POSIX\0job\0control\0(34)
  874. \0\012\0\0U\0\0yale!bronson!tan@uunet.UU.NET
  875. \0\013\0\0U\0\0brett@rpi.edu,\0zsh\0(36)
  876. \0\014\0\0S\0\0subbarao,\0zsh\0sucks!!!!\0(286)
  877. \0\015\0\0U\0\0snibru!d241s008!d241s013!ala@relay.EU.net,\0zsh\0(165)
  878. \0\016\0\0U\0\0nytim!tim@uunet.UU.NET,\0Re:\0Zsh\0on\0Sparc1\0/SunOS\04.0.3
  879. \0\017\0\0U\0\0subbarao,\0zsh\0is\0a\0junk\0shell\0(43)
  880. \0\018\0\0U\0\0amaranth@vela.acs.oakland.edu,\0zsh\0(33)
  881. 43u/84\01:\0x
  882. %\0ls\0-l\0/tmp/zsha06024
  883. /tmp/zsha06024\0not\0found
  884. .De
  885. Note that the shell creates a temporary file, and deletes it
  886. when the command is finished.
  887. .Ds
  888. %\0diff\0=(ls)\0=(ls\0-F)
  889. 3c3
  890. <\0fortune
  891. ---
  892. >\0fortune*
  893. 10c10
  894. <\0strfile
  895. ---
  896. >\0strfile*
  897. .De
  898. If you read \fBzsh\fP's man page, you may notice that \fC<(\fR...\fC)\fR
  899. is another form of process substitution which is similar to
  900. \fC=(\fR...\fC)\fR.
  901. There is an important difference between the two.
  902. In the \fC<(\fR...\fC)\fR case, the shell creates a named pipe (FIFO)
  903. instead of a file.  This is better, since it does not
  904. fill up the file system; but it does not work in all cases.
  905. In fact, if we had replaced \fC=(\fR...\fC)\fR with \fC<(\fR...\fC)\fR in 
  906. the examples above, all of them would have stopped working
  907. except for \fCfgrep -f <(\fR...\fC)\fR.
  908. You can not edit a pipe, or open it as a mail folder;
  909. \fCfgrep\fP, however, has no problem with reading
  910. a list of words from a pipe.
  911. You may wonder why \fCdiff <(foo) bar\fP doesn't work, since
  912. \fCfoo | diff - bar\fP works; this is because \fCdiff\fP creates
  913. a temporary file if it notices that one of its arguments
  914. is \fC-\fP, and then copies its standard input to the temporary
  915. file.
  916. .PP
  917. \fC>(\fR...\fC)\fR is just like \fC<(\fR...\fC)\fR except that the
  918. command between the parentheses will get its input from the named
  919. pipe.
  920. .Ds
  921. %\0dvips\0-o\0>(lpr)\0zsh.dvi
  922. .De
  923. .Sh "Redirection"
  924. .PP
  925. Apart from all the regular redirections like the Bourne shell has,
  926. \fBzsh\fP can do more.  You can send the output of a command to more
  927. than one file, by specifying more redirections like
  928. .Ds
  929. %\0echo\0Hello\0World\0>file1\0>file2
  930. .De
  931. and the text will end up in both files.  Similarly, you can send the
  932. output to a file and into a pipe:
  933. .Ds
  934. %\0make\0>\0make.log\0|\0grep\0Error
  935. .De
  936. The same goes for input.  You can make the input of a command come
  937. from more than one file.
  938. .Ds
  939. %\0sort\0<file1\0<file2\0<file3
  940. .De
  941. The command will first get the contents of file1 as its standard
  942. input, then those of file2 and finally the contents of file3.  This,
  943. too, works with pipes.
  944. .Ds
  945. %\0cut\0-d:\0-f1\0/etc/passwd\0|\0sort\0<newnames
  946. .De
  947. The sort will get as its standard input first the output of \fCcut\fP
  948. and then the contents of \fCnewnames\fP.
  949. .PP
  950. Suppose you would like to watch the standard output of a command on
  951. your terminal, but want to pipe the standard error to another command.
  952. An easy way to do this in \fBzsh\fP is by redirecting the standard
  953. error using \fC2> >(\fR...\fC)\fR.
  954. .Ds
  955. %\0find\0/\0-name\0games\02>\0>(grep\0-v\0'Permission'\0>\0realerrors)
  956. .De
  957. The above redirection will actually be implemented with a regular
  958. pipe, not a temporary named pipe.
  959. .Sh "Aliasing"
  960. .PP
  961. Often-used commands can be abbreviated with an alias:
  962. .Ds
  963. %\0alias\0uc=uncompress
  964. %\0ls
  965. hanoi.Z
  966. %\0uc\0hanoi
  967. %\0ls
  968. hanoi
  969. .De
  970. or commands with certain desired options:
  971. .Ds
  972. %\0alias\0fm='finger\0-m'
  973. %\0fm\0root
  974. Login\0name:\0root\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0In\0real\0life:\0Operator
  975. Directory:\0/\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0Shell:\0/bin/csh
  976. On\0since\0May\019\010:41:15\0on\0console\0\0\0\0\03\0days\05\0hours\0Idle\0Time
  977. No\0unread\0mail
  978. No\0Plan.
  979.  
  980. %\0alias\0lock='lock\0-p\0-60000'
  981. %\0lock
  982. lock:\0/dev/ttyr4\0on\0phoenix.\0timeout\0in\060000\0minutes
  983. time\0now\0is\0Fri\0May\024\004:23:18\0EDT\01991
  984. Key:\0
  985.  
  986. %\0alias\0l='ls\0-AF'
  987. %\0l\0/
  988. \&.bash_history\0\0\0\0\0\0\0\0\0\0\0\0\0\0kadb*
  989. \&.bashrc\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0lib@
  990. \&.cshrc\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0licensed/
  991. \&.exrc\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0lost+found/
  992. \&.login\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0macsyma
  993. \&\fR...
  994. .De
  995. Aliases can also be used to replace old commands:
  996. .Ds
  997. %\0alias\0grep=egrep\0ps=sps\0make=gmake
  998. %\0alias\0whoami='echo\0root'
  999. %\0whoami
  1000. root
  1001. .De
  1002. or to define new ones:
  1003. .Ds
  1004. %\0cd\0/
  1005. %\0alias\0sz='ls\0-l\0|\0sort\0-n\0+3\0|\0tail\0-10'
  1006. %\0sz
  1007. drwxr-sr-x\0\07\0bin\0\0\0\0\0\0\0\0\0\03072\0May\023\011:59\0etc
  1008. drwxrwxrwx\026\0root\0\0\0\0\0\0\0\0\05120\0May\024\004:20\0tmp
  1009. drwxr-xr-x\0\02\0root\0\0\0\0\0\0\0\0\08192\0Dec\026\019:34\0lost+found
  1010. drwxr-sr-x\0\02\0bin\0\0\0\0\0\0\0\0\014848\0May\023\018:48\0dev
  1011. -r--r--r--\0\01\0root\0\0\0\0\0\0\0140520\0Dec\026\020:08\0boot
  1012. -rwxr-xr-x\0\01\0root\0\0\0\0\0\0\0311172\0Dec\026\020:08\0kadb
  1013. -rwxr-xr-x\0\01\0root\0\0\0\0\0\01209695\0Apr\016\015:33\0vmunix.old
  1014. -rwxr-xr-x\0\01\0root\0\0\0\0\0\01209702\0May\014\019:04\0vmunix
  1015. -rwxr-xr-x\0\01\0root\0\0\0\0\0\01209758\0May\021\012:23\0vmunix.new.kernelmap.old
  1016. -rwxr-xr-x\0\01\0root\0\0\0\0\0\01711848\0Dec\026\020:08\0vmunix.org
  1017. %\0cd
  1018. %\0alias\0rable='ls\0-AFtrd\0*(R)'\0nrable='ls\0-AFtrd\0*(^R)'
  1019. %\0rable
  1020. README\0\0\0\0\0\0func/\0\0\0\0\0\0\0bin/\0\0\0\0\0\0\0\0pub/\0\0\0\0\0\0\0\0News/\0\0\0\0\0\0\0src/
  1021. nicecolors\0\0etc/\0\0\0\0\0\0\0\0scr/\0\0\0\0\0\0\0\0tmp/\0\0\0\0\0\0\0\0iris/\0\0\0\0\0\0\0zsh*
  1022. %\0nrable
  1023. Mailboxes/\0\0mail/\0\0\0\0\0\0\0notes
  1024. .De
  1025. (The pattern \fC*(R)\fP matches all readable files in the current
  1026. directory, and \fC*(^R)\fP matches all unreadable files.)
  1027. .PP
  1028. Most other shells have aliases of this kind (\fIcommand\fP aliases).
  1029. However, \fBzsh\fP also has \fIglobal\fP aliases, which are substituted
  1030. anywhere on a line.
  1031. Global aliases can be used to abbreviate frequently-typed
  1032. usernames, hostnames, etc.
  1033. .Ds
  1034. %\0alias\0-g\0me=pfalstad\0gun=egsirer\0mjm=maruchck
  1035. %\0who\0|\0grep\0me
  1036. pfalstad\0ttyp0\0\0\0May\024\003:39\0\0\0(mickey.Princeton)
  1037. pfalstad\0ttyp5\0\0\0May\024\003:42\0\0\0(mickey.Princeton)
  1038. %\0fm\0gun
  1039. Login\0name:\0egsirer\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0In\0real\0life:\0Emin\0Gun\0Sirer
  1040. Directory:\0/u/egsirer\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0Shell:\0/bin/sh
  1041. Last\0login\0Thu\0May\023\019:05\0on\0ttyq3\0from\0bow.Princeton.ED
  1042. New\0mail\0received\0Fri\0May\024\002:30:28\01991;
  1043. \0\0unread\0since\0Fri\0May\024\002:30:27\01991
  1044. %\0alias\0-g\0phx=phoenix.princeton.edu\0warc=wuarchive.wustl.edu
  1045. %\0ftp\0warc
  1046. Connected\0to\0wuarchive.wustl.edu.
  1047. .De
  1048. Here are some more interesting uses.
  1049. .Ds
  1050. %\0alias\0-g\0M='|\0more'\0GF='|\0fgrep\0-f\0~/.friends'
  1051. %\0who\0M\0\0\0#\0\fIpipes\0the\0output\0of\0\fCwho\fI\0through\0\fCmore
  1052. %\0who\0GF\0\0#\0\fIsee\0if\0your\0friends\0are\0on\fC
  1053. %\0w\0GF\0\0\0\0#\0\fIsee\0what\0your\0friends\0are\0doing
  1054. .De
  1055. Another example makes use of \fBzsh\fP's process substitution.
  1056. If you run NIS, and you miss being able to do this:
  1057. .Ds
  1058. %\0grep\0pfalstad\0/etc/passwd
  1059. .De
  1060. you can define an alias that will seem more natural
  1061. than \fCypmatch pfalstad passwd\fP:
  1062. .Ds
  1063. %\0alias\0-g\0PASS='<(ypcat\0passwd)'
  1064. %\0grep\0pfalstad\0PASS
  1065. pfalstad:*:3564:35:Paul\0John\0Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  1066. .De
  1067. If you're really crazy, you can even call it \fC/etc/passwd\fP:
  1068. .Ds
  1069. %\0alias\0-g\0/etc/passwd='<(ypcat\0passwd)'
  1070. %\0grep\0pfalstad\0/etc/passwd
  1071. pfalstad:*:3564:35:Paul\0John\0Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  1072. .De
  1073. The last example shows one of the perils of global aliases;
  1074. they have a lot of potential to cause confusion.
  1075. For example, if you defined a global alias called \fC|\fP (which is
  1076. possible), \fBzsh\fP would begin to act very strangely; every pipe
  1077. symbol would be replaced with the text of your alias.
  1078. To some extent, global aliases are like macros in C;
  1079. discretion is advised in using them and in choosing names for them.
  1080. Using names in all caps is not a bad idea, especially
  1081. for aliases which introduce shell metasyntax (like \fCM\fP and \fCGF\fP
  1082. above).
  1083. .PP
  1084. Note that \fBzsh\fP aliases are not like csh aliases.  The syntax for
  1085. defining them is different, and they do not have arguments.
  1086. All your favorite csh aliases will probably not work under \fBzsh\fP.
  1087. For example, if you try:
  1088. .Ds
  1089. alias\0rm\0mv\0'\e!*\0/tmp/wastebasket'
  1090. .De
  1091. no aliases will be defined, but \fBzsh\fP will not report an error.
  1092. In csh, this line defines an alias that makes \fCrm\fP safe---files
  1093. that are \fCrm\fP'd will be moved to a temporary directory instead of
  1094. instantly destroyed.  In \fBzsh\fP's syntax, however, this line asks
  1095. the shell to print any existing alias definitions for \fCrm\fP,
  1096. \fCmv\fP, or \fC!*\ /tmp/wastebasket\fP.  Since there are none, most
  1097. likely, the shell will not print anything, although \fCalias\fP will
  1098. return a nonzero exit code.  The proper syntax is this:
  1099. .Ds
  1100. alias\0rm='mv\0\e!*\0/tmp/wastebasket'
  1101. .De
  1102. However, this won't work either:
  1103. .Ds
  1104. %\0rm\0foo.dvi
  1105. zsh:\0no\0matches\0found:\0!*
  1106. .De
  1107. While this makes \fCrm\fP safe, it is certainly not what the user
  1108. intended.  In \fBzsh\fP, you must use a shell function for this:
  1109. .Ds
  1110. %\0unalias\0rm
  1111. %\0rm\0()\0{\0mv\0$*\0/tmp/wastebasket\0}
  1112. %\0rm\0foo.dvi
  1113. %\0ls\0/tmp/wastebasket
  1114. foo.dvi
  1115. .De
  1116. While this is much cleaner and easier to read (I hope you will
  1117. agree), it is not csh-compatible.  Therefore, a script to convert
  1118. csh aliases and variables has been provided.  You should only need to use it
  1119. once, to convert all your csh aliases and parameters to \fBzsh\fP format:
  1120. .Ds
  1121. %\0csh
  1122. csh>\0alias
  1123. l\0\0\0\0\0\0\0ls\0-AF
  1124. more\0\0\0\0less
  1125. on\0\0\0\0\0\0last\0-2\0!:1\0;\0who\0|\0grep\0!:1
  1126. csh>\0exit
  1127. %\0c2z\0>neat_zsh_aliases
  1128. %\0cat\0neat_zsh_aliases
  1129. alias\0l='ls\0-AF'
  1130. alias\0more='less'
  1131. on\0()\0{\0last\0-2\0$1\0;\0who\0|\0grep\0$1\0}
  1132. \&...
  1133. .De
  1134. The first two aliases were converted to regular \fBzsh\fP aliases, while
  1135. the third, since it needed to handle arguments, was converted to
  1136. a function.  \fCc2z\fP can convert most aliases to \fBzsh\fP format without
  1137. any problems.  However, if you're using some really arcane csh tricks,
  1138. or if you have an alias with a name like \fCdo\fP (which is reserved
  1139. in \fBzsh\fP), you may have to fix some of the aliases by hand.
  1140. .PP
  1141. The \fCc2z\fP script checks your csh setup, and produces a list
  1142. of \fBzsh\fP commands which replicate your aliases and parameter settings
  1143. as closely as possible.  You could include its output in your
  1144. startup file, \fC.zshrc\fP.
  1145. .Sh "History"
  1146. .PP
  1147. There are several ways to manipulate history in \fBzsh\fP.
  1148. One way is to use csh-style \fC!\fP history:
  1149. .Ds
  1150. %\0/usr/local/bin/!:0\0!-2*:s/foo/bar/\0>>!$
  1151. .De
  1152. If you don't want to use this, you can turn it off
  1153. by typing \fCsetopt nobanghist\fP.  If you are afraid of accidentally
  1154. executing the wrong command you can set the \fIHISTVERIFY\fP option.
  1155. If this option is set, commands that result from history expansion
  1156. will not be executed immediately, but will be put back into the editor
  1157. buffer for further consideration.
  1158. .PP
  1159. If you're not familiar with \fC!\fP history, here follows some
  1160. explanation.  History substitutions always start with a \fC!\fP,
  1161. commonly called \*Qbang\*U.  After the \fC!\fP comes an (optional)
  1162. designation of which \*Qevent\*U (command) to use, then a colon, and
  1163. then a designation of what word of that command to use.  For example,
  1164. \fC!-\fIn\fR refers to the command \fIn\fP commands ago.
  1165. .Ds
  1166. %\0ls
  1167. foo\0\0bar
  1168. %\0cd\0foo
  1169. %\0!-2
  1170. ls
  1171. baz\0\0bam
  1172. .De
  1173. No word designator was used, which means that the whole command
  1174. referred to was repeated.  Note that the shell will echo the result of
  1175. the history substitution.  The word designator can, among other
  1176. things, be a number indicating the argument to use, where \fC0\fP is
  1177. the command.
  1178. .Ds
  1179. %\0/usr/bin/ls\0foo
  1180. foo
  1181. %\0!:0\0bar
  1182. /usr/bin/ls\0bar
  1183. bar
  1184. .De
  1185. In this example, no event designator was used, which tells \fBzsh\fP
  1186. to use the previous command.  A \fC$\fP specifies the last argument
  1187. .Ds
  1188. %\0mkdir\0/usr/local/lib/emacs/site-lisp/calc
  1189. %\0cd\0!:$
  1190. cd\0/usr/local/lib/emacs/site-lisp/calc
  1191. .De
  1192. If you use more words of the same command, only the first \fC!\fP
  1193. needs an event designator.
  1194. .Ds
  1195. %\0make\0prig\0>>\0make.log
  1196. make:\0***\0No\0rule\0to\0make\0target\0`prig'.\0\0Stop.
  1197. %\0cd\0src
  1198. %\0!-2:0\0prog\0>>\0!:$
  1199. make\0prog\0>>\0make.log
  1200. .De
  1201. This is different from csh, where a bang with no event designator
  1202. always refers to the previous command.  If you actually like this
  1203. behaviour, set the \fICSHJUNKIEHISTORY\fP option.
  1204. .Ds
  1205. %\0setopt\0cshjunkiehistory
  1206. %\0!-2:0\0prog2\0>>\0!:$
  1207. make\0prog2\0>>\0cshjunkiehistory
  1208. .De
  1209. Another way to use history is to use the \fCfc\fP command.  For
  1210. example, if you type an erroneous command:
  1211. .Ds
  1212. %\0for\0i\0in\0`cat\0/etc/clients`\0
  1213. \0do\0
  1214. \0rpu\0$i\0
  1215. \0done
  1216. zsh:\0command\0not\0found:\0rpu
  1217. zsh:\0command\0not\0found:\0rpu
  1218. zsh:\0command\0not\0found:\0rpu
  1219. \&\fR...
  1220. .De
  1221. typing \fCfc\fP will execute an editor on this command, allowing
  1222. you to fix it.  (The default editor is \fCvi\fP, by the way,
  1223. not \fCed\fP).
  1224. .Ds
  1225. %\0fc
  1226. 49
  1227. /rpu/s//rup/p
  1228. \0rup\0$i\0
  1229. w
  1230. 49
  1231. q
  1232. for\0i\0in\0`cat\0/etc/clients`\0
  1233. \0do\0
  1234. \0rup\0$i\0
  1235. \0done
  1236. \0\0\0\0\0\0\0\0beam\0\0\0\0up\0\02\0days,\010:17,\0\0\0\0load\0average:\00.86,\00.80,\00.50
  1237. \0\0\0\0\0\0\0\0\0bow\0\0\0\0up\0\04\0days,\0\08:41,\0\0\0\0load\0average:\00.91,\00.80,\00.50
  1238. \0\0\0\0\0\0\0\0burn\0\0\0\0up\0\0\0\0\0\0\0\0\0\017:18,\0\0\0\0load\0average:\00.91,\00.80,\00.50
  1239. \0\0\0\0\0\0\0burst\0\0\0\0up\0\09\0days,\0\01:49,\0\0\0\0load\0average:\00.95,\00.80,\00.50
  1240. \0\0\0\0\0\0\0\0\0tan\0\0\0\0up\0\0\0\0\0\0\0\0\0\011:14,\0\0\0\0load\0average:\00.91,\00.80,\00.50
  1241. \0\0\0\0\0\0\0bathe\0\0\0\0up\0\03\0days,\017:49,\0\0\0\0load\0average:\01.84,\01.79,\01.50
  1242. \0\0\0\0\0\0\0\0bird\0\0\0\0up\0\01\0day,\0\0\09:13,\0\0\0\0load\0average:\01.95,\01.82,\01.51
  1243. \0\0\0\0\0\0bonnet\0\0\0\0up\0\02\0days,\021:18,\0\0\0\0load\0average:\00.93,\00.80,\00.50
  1244. \&\fR...
  1245. .De
  1246. A variant of the \fCfc\fP command is \fCr\fP, which redoes the last
  1247. command, with optional changes:
  1248. .Ds
  1249. %\0echo\0foo
  1250. foo
  1251. %\0r
  1252. echo\0foo
  1253. foo
  1254.  
  1255. %\0echo\0foo
  1256. foo
  1257. %\0r\0foo=bar
  1258. echo\0bar
  1259. bar
  1260. .De
  1261. .Sh "Command Line Editing"
  1262. .PP
  1263. \fBzsh\fP's command line editor, \fBZLE\fP, is quite powerful.
  1264. It is designed to emulate either emacs or vi; the default
  1265. is emacs.  To set the bindings for vi mode, type \fCbindkey -v\fP.  If
  1266. your \fBEDITOR\fP or \fBVISUAL\fP environment variable is vi,
  1267. \fBzsh\fP will use vi emulation by default.  You can then switch to
  1268. emacs mode with \fCbindkey -e\fP.
  1269. .PP
  1270. In addition to basic editing, the shell allows you to 
  1271. recall previous lines in the history.  In emacs mode,
  1272. this is done with \fI^P\fP (control-P) or (on many terminals) with the
  1273. cursor-up key:
  1274. .Ds
  1275. %\0ls\0~
  1276. -\0\0\0\0\0\0\0\0\0\0\0README\0\0\0\0\0\0file\0\0\0\0\0\0\0\0mail\0\0\0\0\0\0\0\0pub\0\0\0\0\0\0\0\0\0tmp
  1277. Mailboxes\0\0\0bin\0\0\0\0\0\0\0\0\0func\0\0\0\0\0\0\0\0nicecolors\0\0scr\0\0\0\0\0\0\0\0\0zsh
  1278. News\0\0\0\0\0\0\0\0etc\0\0\0\0\0\0\0\0\0iris\0\0\0\0\0\0\0\0notes\0\0\0\0\0\0\0src
  1279. %\0echo\0foobar
  1280. foobar
  1281. %\0\fI^P\fC
  1282. %\0echo\0foobar\fI^P\fC
  1283. %\0ls\0~_
  1284. .De
  1285. Pressing \fI^P\fP once brings up the previous line (\fCecho foobar\fP);
  1286. pressing it again brings up the line before that (\fCls ~\fP).
  1287. The cursor is left at the end of the line, allowing you to
  1288. edit the line if desired before executing it.
  1289. In many cases, \fBZLE\fP eliminates the need for the \fCfc\fP command,
  1290. since it is powerful enough to handle even multiline commands:
  1291. .Ds
  1292. %\0for\0i\0in\0a\0b\0c\0d\0e
  1293. >\0do
  1294. >\0echo\0$i
  1295. >\0done
  1296. a
  1297. b
  1298. c
  1299. d
  1300. e
  1301. %\0\fI^P\fC
  1302. %\0for\0i\0in\0a\0b\0c\0d\0e\0
  1303. \0do\0
  1304. \0echo\0$i\0
  1305. \0done_
  1306. .De
  1307. Now you can just move up to the part you want to change...
  1308. .Ds
  1309. %\0for\0i\0in\0\kxa\l'|\nxu\(ul'\0b\0c\0d\0e
  1310. \0do\0
  1311. \0echo\0$i\0
  1312. \0done
  1313. .De
  1314. change it, and execute the new command.
  1315. .Ds
  1316. %\0for\0i\0in\0f\0g\0h\0i\0j
  1317. \0do\0
  1318. \0echo\0$i\0
  1319. \0done
  1320. f
  1321. g
  1322. h
  1323. i
  1324. j
  1325. .De
  1326. Also, you can search the history for a certain command using
  1327. \fIESC-P\fP, this will look for the last command that started with the
  1328. (part of the) word at the beginning of the current line.  Hitting
  1329. \fIESC-P\fP another time gets you the command before that, etc.
  1330. .Ds
  1331. %\0set\0\fIESC-P\fC
  1332. %\0setopt\0autolist\0\fIESC-P\fC
  1333. %\0setopt\0nocorrect_
  1334. .De
  1335. Another way is to do an incremental search, emacs-style:
  1336. .Ds
  1337. %\0\fI^R\fC
  1338. %\0_
  1339. i-search:
  1340.  
  1341. %\0l\kxs\l'|\nxu\(ul'\0/usr/bin
  1342. i-search:\0l
  1343.  
  1344. %\0date\0>\0foofile\kx.\l'|\nxu\(ul'c
  1345. i-search:\0le
  1346. .De
  1347. Suppose you have retrieved an old history event in one of these ways
  1348. and would like to execute several consecutive old commands starting
  1349. with this one.  \fC^O\fP will execute the current command and then put
  1350. the next command from the history into the editor buffer.  Typing
  1351. \fC^O\fP several times will therefore reexecute several consecutive
  1352. commands from the history.  Of course, you can edit some of those
  1353. commands in between.
  1354. .PP
  1355. In addition to completion (see below), \fITAB\fP performs expansion if
  1356. possible.
  1357. .Ds
  1358. %\0ls\0*.c\fITAB\fC
  1359. %\0ls\0foofile.c\0fortune.c\0rnd.c\0strfile.c\0unstr.c_
  1360. .De
  1361. For example, suppose you have a bunch of weird files in an important
  1362. directory:
  1363. .Ds
  1364. %\0ls
  1365. \0\0*\0*\0*\0\0\0\0\0\0\0;\0&\0%\0$??foo\0\0dspfok\0\0\0\0\0\0\0\0foo.c
  1366. \0\0!"foo"!\0\0\0\0\0\0\0`\0\e\0`\0\0\0\0\0\0\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0rrr
  1367. .De
  1368. You want to remove them, but you don't want to damage \fCfoo.c\fP.
  1369. Here is one way to do this:
  1370. .Ds
  1371. %\0rm\0*\fITAB\fC
  1372. %\0rm\0\e\0\e\0\e*\e\0\e*\e\0\e*\e\0\e\0\e\0\0\e!\e"foo\e"\e!\0\e;\e\0\e&\e\0%\e\0\e$'
  1373. ''
  1374. 'foo\0\e`\e\0\e\e\e\0\e`\0dspfok\0foo\0foo.c\0rrr_
  1375. .De
  1376. When you expand \fC*\fP, \fBzsh\fP inserts the names of all the files
  1377. into the editing buffer, with proper shell quoting.
  1378. Now, just move back and remove \fCfoo.c\fP from the buffer:
  1379. .Ds
  1380. %\0rm\0\e\0\e\0\e*\e\0\e*\e\0\e*\e\0\e\0\e\0\0\e!\e"foo\e"\e!\0\e;\e\0\e&\e\0%\e\0\e$'
  1381. ''
  1382. 'foo\0\e`\e\0\e\e\e\0\e`\0dspfok\0foo\0\kxr\l'|\nxu\(ul'rr
  1383. .De
  1384. and press return.
  1385. Everything except \fCfoo.c\fP will be deleted from the directory.  If
  1386. you do not want to actually expand the current word, but would like to
  1387. see what the matches are, type \fC^Xg\fP.
  1388. .Ds
  1389. %\0rm\0f*\fI^Xg\fP
  1390. foo\0\0\0\0foo.c
  1391. %\0rm\0f*_
  1392. .De
  1393. Here's another trick; let's say you have typed this command in:
  1394. .Ds
  1395. %\0gcc\0-o\0x.out\0foob.c\0-g\0-Wpointer-arith\0-Wtrigraphs_
  1396. .De
  1397. and you forget which library you want.  You need to escape
  1398. out for a minute and check by typing
  1399. \fCls /usr/lib\fP, or some other such command;
  1400. but you don't want to retype the whole command again,
  1401. and you can't press return now because the current command
  1402. is incomplete.
  1403. In \fBzsh\fP, you can put the line on the \fIbuffer stack\fP, using
  1404. \fIESC-Q\fP, and type some other commands.  The next time a prompt is printed,
  1405. the \fCgcc\fP line will be popped off the stack and put
  1406. in the editing buffer automatically; you can then enter the
  1407. proper library name and press return (or, \fIESC-Q\fP again and look
  1408. for some other libraries whose names you forgot).
  1409. .PP
  1410. A similar situation: what if you forget the option to gcc that
  1411. finds bugs using AI techniques?  You could either use \fIESC-Q\fP
  1412. again, and type \fCman gcc\fP, or you could press \fIESC-H\fP, which
  1413. essentially does the same thing; it puts the current line on
  1414. the buffer stack, and executes the command \fCrun-help gcc\fP,
  1415. where \fCrun-help\fP is an alias for \fCman\fP.
  1416. .PP
  1417. Another interesting command is \fIESC-A\fP.  This executes the
  1418. current line, but retains it in the buffer, so that it appears
  1419. again when the next prompt is printed.
  1420. Also, the cursor stays in the same place.
  1421. This is useful for executing a series of similar commands:
  1422. .Ds
  1423. %\0cc\0grok.c\0-g\0-lc\0-lgl\0-lsun\0-lmalloc\0-Bstatic\0-o\0b.out
  1424. %\0cc\0fubar.c\0-g\0-lc\0-lgl\0-lsun\0-lmalloc\0-Bstatic\0-o\0b.out
  1425. %\0cc\0fooble.c\0-g\0-lc\0-lgl\0-lsun\0-lmalloc\0-Bstatic\0-o\0b.out
  1426. .De
  1427. .PP
  1428. The \fIESC-'\fP command is useful for managing the shell's quoting
  1429. conventions.  Let's say you want to print this string:
  1430. .Ds
  1431. don't\0do\0that;\0type\0'rm\0-rf\0\e*',\0with\0a\0\e\0before\0the\0*.
  1432. .De
  1433. All that is necessary is to type it into the editing buffer:
  1434. .Ds
  1435. %\0don't\0do\0that;\0type\0'rm\0-rf\0\e*',\0with\0a\0\e\0before\0the\0*.
  1436. .De
  1437. press \fIESC-'\fP (escape-quote):
  1438. .Ds
  1439. %\0'don'\e''t\0do\0that;\0type\0'\e''rm\0-rf\0\e*'\e'',\0with\0a\0\e\0before\0the\0*.'
  1440. .De
  1441. then move to the beginning and add the \fCecho\fP command.
  1442. .Ds
  1443. %\0echo\0'don'\e''t\0do\0that;\0type\0'\e''rm\0-rf\0\e*'\e'',\0with\0a\0\e\0before\0the\0*.'
  1444. don't\0do\0that;\0type\0'rm\0-rf\0\e*',\0with\0a\0\e\0before\0the\0*.
  1445. .De
  1446. Let's say you want to create an alias to do this \fCecho\fP command.
  1447. This can be done by recalling the line with \fI^P\fP and pressing
  1448. \fIESC-'\fP again:
  1449. .Ds
  1450. %\0'echo\0'\e''don'\e''\e'\e'''\e''t\0do\0that;\0type\0'\e''\e'\e'''\e''rm\0-rf
  1451. \e*'\e''\e'\e'''\e'',\0with\0a\0\e\0before\0the\0*.'\e'''
  1452. .De
  1453. and then move to the beginning and add the command to create
  1454. an alias.
  1455. .Ds
  1456. %\0alias\0zoof='echo\0'\e''don'\e''\e'\e'''\e''t\0do\0that;\0type\0'\e''\e'\e'''\e''rm
  1457. -rf\0\e*'\e''\e'\e'''\e'',\0with\0a\0\e\0before\0the\0*.'\e'''
  1458. %\0zoof
  1459. don't\0do\0that;\0type\0'rm\0-rf\0\e*',\0with\0a\0\e\0before\0the\0*.
  1460. .De
  1461. If one of these fancy editor commands changes your command line in a
  1462. way you did not intend, you can undo changes with \fC^_\fP, if you can
  1463. get it out of your keyboard, or \fC^X^U\fP, otherwise.
  1464. .PP
  1465. Another use of the editor is to edit the value of variables.
  1466. For example, an easy way to change your path is to use
  1467. the \fCvared\fP command:
  1468. .Ds
  1469. %\0vared\0PATH
  1470. >\0/u/pfalstad/scr:/u/pfalstad/bin/sun4:/u/maruchck/scr:/u/subbarao/bin:/u/maruc
  1471. hck/bin:/u/subbarao/scripts:/usr/princeton/bin:/usr/ucb:/usr/bin:/bin:/usr/host
  1472. s:/usr/princeton/bin/X11:/./usr/lang:/./usr/etc:/./etc
  1473. .De
  1474. You can now edit the path.  When you press return, the contents
  1475. of the edit buffer will be assigned to \fBPATH\fP.
  1476. .Sh "Completion"
  1477. .PP
  1478. Another great \fBzsh\fP feature is completion.  If you hit \fITAB\fP, \fBzsh\fP
  1479. will complete all kinds of stuff.  Like commands or filenames:
  1480. .Ds
  1481. %\0comp\fITAB\fC
  1482. %\0compress\0_
  1483.  
  1484. %\0ls\0nic\fITAB\fC
  1485. %\0ls\0nicecolors\0_
  1486.  
  1487. %\0ls\0/usr/pr\fITAB\fC
  1488. %\0ls\0/usr/princeton/_
  1489.  
  1490. %\0ls\0-l\0=com\fITAB\fC
  1491. %\0ls\0-l\0=compress\0_
  1492. .De
  1493. If the completion is ambiguous, the editor will beep.  If you find
  1494. this annoying, you can set the \fINOLISTBEEP\fP option.  Completion
  1495. can even be done in the middle of words.  To use this, you will have
  1496. to set the \fICOMPLETEINWORD\fP option:
  1497. .Ds
  1498. %\0setopt\0completeinword
  1499. %\0ls\0/usr/p\kxt\l'|\nxu\(ul'on\fITAB\fC
  1500. %\0ls\0/usr/prince\kxt\l'|\nxu\(ul'on/
  1501. %\0setopt\0alwaystoend
  1502. %\0ls\0/usr/p\kxt\l'|\nxu\(ul'on\fITAB\fC
  1503. %\0ls\0/usr/princeton/_
  1504. .De
  1505. You can list possible completions by pressing \fI^D\fP:
  1506. .Ds
  1507. %\0ls\0/vmu\fITAB\0\(embeep\(em\fC
  1508. %\0ls\0/vmunix_
  1509. %\0ls\0/vmunix\fI^D\fC
  1510. vmunix\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0vmunix.old\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
  1511. vmunix.new.kernelmap.old\0\0vmunix.org
  1512. .De
  1513. Or, you could just set the \fIAUTOLIST\fP option:
  1514. .Ds
  1515. %\0setopt\0autolist
  1516. %\0ls\0/vmu\fITAB\0\(embeep\(em\fC
  1517. vmunix\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0vmunix.old\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
  1518. vmunix.new.kernelmap.old\0\0vmunix.org
  1519. %\0ls\0/vmunix_
  1520. .De
  1521. If you like to see the types of the files in these lists, like in
  1522. \fCls\ -F\fP, you can set the \fILISTTYPES\fP option.  Together with
  1523. \fIAUTOLIST\fP you can use \fILISTAMBIGUOUS\fP.  This will only list
  1524. the possibilities if there is no unambiguous part to add:
  1525. .Ds
  1526. %\0setopt\0listambiguous
  1527. %\0ls\0/vmu\fITAB\0\(embeep\(em\fC
  1528. %\0ls\0/vmunix_\fITAB\0\(embeep\(em\fC
  1529. vmunix\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0vmunix.old\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
  1530. vmunix.new.kernelmap.old\0\0vmunix.org
  1531. .De
  1532. If you don't want several of these listings to scroll the screen so
  1533. much, the \fIALWAYSLASTPROMPT\fP option is useful.  If set, you can
  1534. continue to edit the line you were editing, with the completion
  1535. listing appearing beneath it.
  1536. .PP
  1537. Another interesting option is \fIMENUCOMPLETE\fP.  This affects the
  1538. way \fITAB\fP works.  Let's look at the \fC/vmunix\fP example again:
  1539. .Ds
  1540. %\0setopt\0menucomplete
  1541. %\0ls\0/vmu\fITAB\fC
  1542. %\0ls\0/vmunix\fITAB\fC
  1543. %\0ls\0/vmunix.new.kernelmap.old\fITAB\fC
  1544. %\0ls\0/vmunix.old_
  1545. .De
  1546. Each time you press \fITAB\fP, it displays the next possible completion.
  1547. In this way, you can cycle through the possible completions until
  1548. you find the one you want.
  1549. .PP
  1550. The \fIAUTOMENU\fP option makes a nice compromise between this method
  1551. of completion and the regular method.  If you set this option,
  1552. pressing \fITAB\fP once completes the unambiguous part normally,
  1553. pressing the \fITAB\fP key repeatedly after an ambiguous completion
  1554. will cycle through the possible completions.
  1555. .PP
  1556. Another option you could set is \fIRECEXACT\fP, which causes
  1557. exact matches to be accepted, even if there are other possible
  1558. completions:
  1559. .Ds
  1560. %\0setopt\0recexact
  1561. %\0ls\0/vmu\fITAB\0\(embeep\(em\fC
  1562. vmunix\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0vmunix.old\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
  1563. vmunix.new.kernelmap.old\0\0vmunix.org
  1564. %\0ls\0/vmunix_\fITAB\fC
  1565. %\0ls\0/vmunix\0_
  1566. .De
  1567. To facilitate the typing of pathnames, a slash will be added whenever
  1568. a directory is completed.  Some computers don't like the spurious
  1569. slashes at the end of directory names.  In that case, the
  1570. \fIAUTOREMOVESLASH\fP option comes to rescue.  It will remove these
  1571. slashes when you type a space or return after them.
  1572. .PP
  1573. The \fIfignore\fP variable lists suffixes of files to ignore
  1574. during completion.
  1575. .Ds
  1576. %\0ls\0foo\fITAB\0\(embeep\(em\fC
  1577. foofile.c\0\0foofile.o
  1578. %\0fignore=(\0.o\0\e~\0.bak\0.junk\0)
  1579. %\0ls\0foo\fITAB\fP
  1580. %\0ls\0foofile.c\0_
  1581. .De
  1582. Since \fCfoofile.o\fP has a suffix that is in the \fCfignore\fP list,
  1583. it was not considered a possible completion of \fCfoo\fP.
  1584. .PP
  1585. Username completion is also supported:
  1586. .Ds
  1587. %\0ls\0~pfal\fITAB\fC
  1588. %\0ls\0~pfalstad/_
  1589. .De
  1590. and parameter name completion:
  1591. .Ds
  1592. %\0echo\0$ORG\fITAB\fC
  1593. %\0echo\0$ORGANIZATION\0_
  1594. %\0echo\0${ORG\fITAB\fC
  1595. %\0echo\0${ORGANIZATION\0_
  1596. .De
  1597. Note that in the last example a space is added after the completion as
  1598. usual.  But if you want to add a colon or closing brace, you probably
  1599. don't want this extra space.  Setting the \fIAUTOPARAMKEYS\fP option
  1600. will automatically remove this space if you type a colon or closing
  1601. brace after such a completion.
  1602. .PP
  1603. There is also option completion:
  1604. .Ds
  1605. %\0setopt\0nocl\fITAB\fC
  1606. %\0setopt\0noclobber\0_
  1607. .De
  1608. and binding completion:
  1609. .Ds
  1610. %\0bindkey\0'^X^X'\0pu\fITAB\fC
  1611. %\0bindkey\0'^X^X'\0push-line\0_
  1612. .De
  1613. The \fCcompctl\fP command is used to control completion of the
  1614. arguments of specific commands.  For example, to specify that certain
  1615. commands take other commands as arguments, you use \fCcompctl -c\fP:
  1616. .Ds
  1617. %\0compctl\0-c\0man\0nohup
  1618. %\0man\0upt\fITAB\fC
  1619. %\0man\0uptime\0_
  1620. .De
  1621. To specify that a command should complete filenames, you should use
  1622. \fCcompctl -f\fP.  This is the default.  It can be combined with \fC-c\fP,
  1623. as well.
  1624. .Ds
  1625. %\0compctl\0-cf\0echo
  1626. %\0echo\0upt\fITAB\fC
  1627. %\0echo\0uptime\0_
  1628.  
  1629. %\0echo\0fo\fITAB\fC
  1630. %\0echo\0foo.c
  1631. .De
  1632. Similarly, use \fC-o\fP to specify options, \fC-v\fP to specify
  1633. variables, and \fC-b\fP to specify bindings.
  1634. .Ds
  1635. %\0compctl\0-o\0setopt\0unsetopt
  1636. %\0compctl\0-v\0typeset\0vared\0unset\0export
  1637. %\0compctl\0-b\0bindkey
  1638. .De
  1639. You can also use \fC-k\fP to specify a custom list of keywords to use
  1640. in completion.  After the \fC-k\fP comes either the name of an array
  1641. or a literal array to take completions from.
  1642. .Ds
  1643. %\0ftphosts=(ftp.uu.net\0wuarchive.wustl.edu)
  1644. %\0compctl\0-k\0ftphosts\0ftp
  1645. %\0ftp\0wu\fITAB\fC
  1646. %\0ftp\0wuarchive.wustl.edu\0_
  1647.  
  1648. %\0compctl\0-k\0'(cpirazzi\0subbarao\0sukthnkr)'\0mail\0finger
  1649. %\0finger\0cp\fITAB\fC
  1650. %\0finger\0cpirazzi\0_
  1651. .De
  1652. To better specify the files to complete for a command, use the
  1653. \fC-g\fP option which takes any glob pattern as an argument.  Be sure
  1654. to quote the glob patterns as otherwise they will be expanded when the
  1655. \fCcompctl\fP command is run.
  1656. .Ds
  1657. %\0ls
  1658. letter.tex\0\0letter.dvi\0\0letter.aux\0\0letter.log\0\0letter.toc
  1659. %\0compctl\0-g\0'*.tex'\0latex
  1660. %\0compctl\0-g\0'*.dvi'\0xdvi\0dvips
  1661. %\0latex\0l\fITAB\fC
  1662. %\0latex\0letter.tex\0_
  1663. %\0xdvi\0l\fITAB\fC
  1664. %\0xdvi\0letter.dvi\0_
  1665. .De
  1666. Glob patterns can include qualifiers within parentheses.  To rmdir
  1667. only directories and cd to directories and symbolic links pointing to
  1668. them:
  1669. .Ds
  1670. %\0compctl\0-g\0'*(-/)'\0cd
  1671. %\0compctl\0-g\0'*(/)'\0rmdir
  1672. .De
  1673. RCS users like to run commands on files which are not in the current
  1674. directory, but in the RCS subdirectory where they all get \fC,v\fP
  1675. suffixes.  They might like to use
  1676. .Ds
  1677. %\0compctl\0-g\0'RCS/*(:t:s/\e,v//)'\0co\0rlog\0rcs
  1678. %\0ls\0RCS
  1679. builtin.c,v\0\0lex.c,v\0\0\0\0\0\0zle_main.c,v
  1680. %\0rlog\0bu\fITAB\fC
  1681. %\0rlog\0builtin.c\0_
  1682. .De
  1683. The \fC:t\fP modifier keeps only the last part of the pathname and the
  1684. \fC:s/\e,v//\fP will replace any \fC,v\fP by nothing.
  1685. .PP
  1686. The \fC-s\fP flag is similar to \fC-g\fP, but it uses all expansions,
  1687. instead of just globbing, like brace expansion, parameter substitution
  1688. and command substitution.
  1689. .Ds
  1690. %\0compctl\0-s\0'$(setopt)'\0unsetopt
  1691. .De
  1692. will only complete options which are actually set to be arguments to
  1693. \fCunsetopt\fP.
  1694. .PP
  1695. Sometimes a command takes another command as its argument.  You can
  1696. tell \fBzsh\fP to complete commands as the first argument to such a
  1697. command and then use the completion method of the second command.  The
  1698. \fC-l\fP flag with a null-string argument is used for this.
  1699. .Ds
  1700. %\0compctl\0-l\0''\0nohup\0exec
  1701. %\0nohup\0comp\fITAB\fC
  1702. %\0nohup\0compress\0_
  1703. %\0nohup\0compress\0fil\fITAB\fC
  1704. %\0nohup\0compress\0filename\0_
  1705. .De
  1706. Sometimes you would like to run really complicated commands to find
  1707. out what the possible completions are.  To do this, you can specify a
  1708. shell function to be called that will assign the possible completions
  1709. to a variable called reply.  Note that this variable must be an array.
  1710. Here's another (much slower) way to get the completions for \fCco\fP
  1711. and friends:
  1712. .Ds
  1713. %\0function\0getrcs\0{
  1714. >\0reply=()
  1715. >\0for\0i\0in\0RCS/*
  1716. >\0\0\0do
  1717. >\0\0\0reply=($reply[*]\0$(basename\0$i\0,v))
  1718. >\0\0\0done
  1719. >\0}
  1720. %\0compctl\0-K\0getrcs\0co\0rlog\0rcs
  1721. .De
  1722. Some command arguments use a prefix that is not a part of the things
  1723. to complete.  The kill builtin command takes a signal name after a
  1724. \fC-\fP.  To make such a prefix be ignored in the completion process,
  1725. you can use the \fC-P\fP flag.
  1726. .Ds
  1727. %\0compctl\0-P\0-\0-k\0signals\0kill
  1728. %\0kill\0-H\fITAB\fP
  1729. %\0kill\0-HUP\0_
  1730. .De
  1731. TeX is usually run on files ending in \fC.tex\fP, but also sometimes
  1732. on other files.  It is somewhat annoying to specify that the arguments
  1733. of TeX should end in \fC.tex\fP and then not be able to complete these
  1734. other files.  Therefore you can specify things like \*QComplete to
  1735. files ending in \fC.tex\fP if available, otherwise complete to any
  1736. filename.\*U.  This is done with \fIxor\fPed completion:
  1737. .Ds
  1738. %\0compctl\0-g\0'*.tex'\0+\0-f\0tex
  1739. .De
  1740. The \fC+\fP tells the editor to only take the next thing into account
  1741. if the current one doesn't generate any matches.  If you have not
  1742. changed the default completion, the above example is in fact
  1743. equivalent to
  1744. .Ds
  1745. %\0compctl\0-g\0'*.tex'\0+\0tex
  1746. .De
  1747. as a lone \fC+\fP at the end is equivalent to specifying the default
  1748. completion after the \fC+\fP.  This form of completion is also
  1749. frequently used if you want to run some command only on a certain type
  1750. of files, but not necessarily in the current directory.  In this case
  1751. you will want to complete both files of this type and directories.
  1752. Depending on your preferences you can use either of
  1753. .Ds
  1754. %\0compctl\0-g\0'*.ps'\0+\0-g\0'*(-/)'\0ghostview
  1755. %\0compctl\0-g\0'*.ps\0*(-/)'\0ghostview
  1756. .De
  1757. where the first one will only complete directories (and symbolic links
  1758. pointing to directories) if no postscript file matches the already
  1759. typed part of the argument.
  1760. .Sh "Extended completion"
  1761. .PP
  1762. If you play with completion, you will soon notice that you would like
  1763. to specify what to complete, depending on what flags you give to the
  1764. command and where you are on the command line.  For example, a command
  1765. could take any filename argument after a \fC-f\fP flag, a username
  1766. after a \fC-u\fP flag and an executable after a \fC-x\fP flag.  This
  1767. section will introduce you to the ways to specify these things.  To
  1768. many people it seems rather difficult at first, but taking the trouble
  1769. to understand it can save you lots of typing in the end.  Even I keep
  1770. being surprised when \fBzsh\fP manages to complete a small or even
  1771. empty prefix to the right file in a large directory.
  1772. .PP
  1773. To tell \fBzsh\fP about these kinds of completion, you use \*Qextended
  1774. completion\*U by specifying the \fC-x\fP flag to compctl.  The
  1775. \fC-x\fP flag takes a list of patterns/flags pairs.  The patterns
  1776. specify when to complete and the flags specify what.  The flags are
  1777. simply those mentioned above, like \fC-f\fP or \fC-g \fIglob
  1778. pattern\fR.
  1779. .PP
  1780. As an example, the \fCr[\fIstring1\fC,\fIstring2\fC]\fR pattern
  1781. matches if the cursor is after something that starts with
  1782. \fIstring1\fP and before something that starts with \fIstring2\fP.
  1783. The \fIstring2\fP is often something that you do not want to match
  1784. anything at all.
  1785. .Ds
  1786. %\0ls
  1787. foo1\0\0\0bar1\0\0\0foo.Z\0\0bar.Z
  1788. %\0compctl\0-g\0'^*.Z'\0-x\0'r[-d,---]'\0-g\0'*.Z'\0--\0compress
  1789. %\0compress\0f\fITAB\fP
  1790. %\0compress\0foo1\0_
  1791. %\0compress\0-d\0f\fITAB\fP
  1792. %\0compress\0-d\0foo.Z\0_
  1793. .De
  1794. In the above example, if the cursor is after the \fC-d\fP the pattern
  1795. will match and therefore \fBzsh\fP uses the \fC-g *.Z\fP flag that will only
  1796. complete files ending in \fC.Z\fP.  Otherwise, if no pattern matches,
  1797. it will use the flags before the \fC-x\fP and in this case complete
  1798. every file that does not end in \fC.Z\fP.
  1799. .PP
  1800. The \fCs[\fIstring\fC]\fR pattern matches if the current word starts
  1801. with \fIstring\fP.  The \fIstring\fP itself is not considered to be
  1802. part of the completion.
  1803. .Ds
  1804. %\0compctl\0-x\0's[-]'\0-k\0signals\0--\0kill
  1805. %\0kill\0-H\fITAB\fP
  1806. %\0kill\0-HUP\0_
  1807. .De
  1808. The \fCtar\fP command takes a tar file as an argument after the
  1809. \fC-f\fP option.  The \fCc[\fIoffset\fC,\fIstring\fC]\fR pattern
  1810. matches if the word in position \fIoffset\fP relative to the current
  1811. word is \fIstring\fP.  More in particular, if \fIoffset\fP is -1, it
  1812. matches if the previous word is \fIstring\fP.  This suggests
  1813. .Ds
  1814. %\0compctl\0-f\0-x\0'c[-1,-f]'\0-g\0'*.tar'\0--\0tar
  1815. .De
  1816. But this is not enough.  The \fC-f\fP option could be the last of a
  1817. longer string of options.  \fCC[\fR...\fC,\fR...\fC]\fR is just like
  1818. \fCc[\fR...\fC,\fR...\fC]\fR, except that it uses glob-like pattern
  1819. matching for \fIstring\fP.  So
  1820. .Ds
  1821. %\0compctl\0-f\0-x\0'C[-1,-*f]'\0-g\0'*.tar'\0--\0tar
  1822. .De
  1823. will complete tar files after any option string ending in an \fCf\fP.
  1824. But we'd like even more.  Old versions of tar used all options as the
  1825. first argument, but without the minus sign.  This might be
  1826. inconsistent with option usage in all other commands, but it is still
  1827. supported by newer versions of \fCtar\fP.  So we would also like to
  1828. complete tar files if the first argument ends in an \fCf\fP and we're
  1829. right behind it.
  1830. .PP
  1831. We can `and' patterns by putting them next to each other with a space
  1832. between them.  We can `or' these sets by putting comma's between them.
  1833. We will also need some new patterns.  \fCp[\fInum\fC]\fR will match if
  1834. the current argument (the one to be completed) is the \fInum\fPth
  1835. argument.  \fCW[\fIindex\fC,\fIpattern\fC]\fR will match if the
  1836. argument in place \fIindex\fP matches the \fIpattern\fP.  This gives
  1837. us
  1838. .Ds
  1839. %\0compctl\0-f\0-x\0'C[-1,-*f]\0,\0W[1,*f]\0p[2]'\0-g\0'*.tar'\0--\0tar
  1840. .De
  1841. In words: If the previous argument is an option string that ends in an
  1842. \fCf\fP, or the first argument ended in an \fCf\fP and it is now the
  1843. second argument, then complete only filenames ending in \fC.tar\fP.
  1844. .PP
  1845. All the above examples used only one set of patterns with one
  1846. completion flag.  You can use several of these pattern/flag pairs
  1847. separated by a \fC-\fP.  The first matching pattern will be used.
  1848. Suppose you have a version of \fCtar\fP that supports compressed files
  1849. by using a \fC-Z\fP option.  Leaving the old tar syntax aside for a
  1850. moment, we would like to complete files ending in \fC.tar.Z\fP if a
  1851. \fC-Z\fP option has been used and files ending in \fC.tar\fP
  1852. otherwise, all this only after a \fC-f\fP flag.  Again, the \fC-Z\fP
  1853. can be alone or it can be part of a longer option string, perhaps the
  1854. same as that of the \fC-f\fP flag.  Here's how to do it; note the
  1855. backslash and the secondary prompt which are not part of the
  1856. \fCcompctl\fP command.
  1857. .Ds
  1858. %\0compctl\0-f\0-x\0'C[-1,-*Z*f]\0,\0R[-*Z*,---]\0C[-1,-*f]'\0-g\0'*.tar.Z'\0-\0\e
  1859. >\0'C[-1,-*f]'\0-g\0'*.tar'\0--\0tar
  1860. .De
  1861. The first pattern set tells us to match if either the previous
  1862. argument was an option string including a \fCZ\fP and ending in an
  1863. \fCf\fP or there was an option string with a \fCZ\fP somewhere and the
  1864. previous word was any option string ending in an \fCf\fP.  If this is
  1865. the case, we need a compressed tar file.  Only if this is not the case
  1866. the second pattern set will be considered.  By the way,
  1867. \fCR[\fIpattern1\fC,\fIpattern2\fC]\fR is just like
  1868. \fCr[\fR...\fC,\fR...\fC]\fR except that it uses pattern matching with
  1869. shell metacharacters instead of just strings.
  1870. .PP
  1871. You will have noticed the \fC--\fP before the command name.  This ends
  1872. the list of pattern/flag pairs of \fC-x\fP.  It is usually used just
  1873. before the command name, but you can also use an extended completion
  1874. as one part of a list of xored completions, in which case the \fC--\fP
  1875. appears just before one of the \fC+\fP signs.
  1876. .PP
  1877. Note the difference between using extended completion as part of a
  1878. list of xored completions as in
  1879. .Ds
  1880. %\0ls
  1881. foo\0\0bar
  1882. %\0compctl\0-x\0'r[-d,---]'\0-g\0'*.Z'\0--\0+\0-g\0'^*.Z'\0compress
  1883. %\0compress\0-d\0f\fITAB\fP
  1884. %\0compress\0-d\0foo\0_
  1885. .De
  1886. and specifying something before the \fC-x\fP as in
  1887. .Ds
  1888. %\0compctl\0-g\0'^*.Z'\0-x\0'r[-d,---]'\0-g\0'*.Z'\0--\0compress
  1889. %\0compress\0-d\0f\fITAB\fP
  1890. %\0compress\0-d\0f_
  1891. .De
  1892. In the first case, the alternative glob pattern (\fC^*.Z\fP) will be
  1893. used if the first part does not generate any possible completions,
  1894. while in the second case the alternative glob pattern will only be
  1895. used if the \fCr[\fR...\fC]\fR pattern doesn't match.
  1896. .Sh "Bindings"
  1897. .PP
  1898. Each of the editor commands we have seen was actually a function bound
  1899. by default to a certain key.  The real names of the commands are:
  1900. .Ds
  1901. \fCexpand-or-complete\0\0\0\fITAB\fR
  1902. \fCpush-line\0\0\0\0\0\0\0\0\0\0\0\0\fIESC-Q\fR
  1903. \fCrun-help\0\0\0\0\0\0\0\0\0\0\0\0\0\fIESC-H\fR
  1904. \fCaccept-and-hold\0\0\0\0\0\0\fIESC-A\fR
  1905. \fCquote-line\0\0\0\0\0\0\0\0\0\0\0\fIESC-'\fR
  1906. .De
  1907. These bindings are arbitrary; you could change them if you want.
  1908. For example, to bind \fCaccept-line\fP to \fI^Z\fP:
  1909. .Ds
  1910. %\0bindkey\0'^Z'\0accept-line
  1911. .De
  1912. Another idea would be to bind the delete key to \fCdelete-char\fP;
  1913. this might be convenient if you use \fI^H\fP for backspace.
  1914. .Ds
  1915. %\0bindkey\0'^?'\0delete-char
  1916. .De
  1917. Or, you could bind \fI^X\fP\fI^H\fP to \fCrun-help\fP:
  1918. .Ds
  1919. %\0bindkey\0'^X^H'\0run-help
  1920. .De
  1921. Other examples:
  1922. .Ds
  1923. %\0bindkey\0'^X^Z'\0universal-argument
  1924. %\0bindkey\0'\0'\0magic-space
  1925. %\0bindkey\0-s\0'^T'\0'uptime
  1926. >\0'
  1927. %\0bindkey\0'^Q'\0push-line-or-edit
  1928. .De
  1929. \fCuniversal-argument\fP multiplies the next command by 4.
  1930. Thus \fI^X\fP\fI^Z\fP\fI^W\fP might delete the last four words on the line.
  1931. If you bind space to \fCmagic-space\fP, then csh-style history
  1932. expansion is done on the line whenever you press the space bar.
  1933. .PP
  1934. Something that often happens is that I am typing a multiline command
  1935. and discover an error in one of the previous lines.  In this case,
  1936. \fCpush-line-or-edit\fP will put the entire multiline construct into
  1937. the editor buffer.  If there is only a single line, it is equivalent
  1938. to \fCpush-line\fP.
  1939. .PP
  1940. The \fC-s\fP flag to \fCbindkey\fP specifies that you are binding the key
  1941. to a string, not a command.  Thus \fCbindkey -s '^T' 'uptime\en'\fP
  1942. lets you VMS lovers get the load average whenever you press \fI^T\fP.
  1943. .PP
  1944. If you have a NeXT keyboard, the one with the \fC|\fP and \fC\e\fP keys
  1945. very inconveniently placed, the following
  1946. bindings may come in handy:
  1947. .Ds
  1948. %\0bindkey\0-s\0'\ee/'\0'\e\e'
  1949. %\0bindkey\0-s\0'\ee='\0'|'
  1950. .De
  1951. Now you can type \fIALT-/\fP to get a backslash, and \fIALT-=\fP to
  1952. get a vertical bar.  This only works inside \fBzsh\fP, of course;
  1953. \fCbindkey\fP has no effect on the key mappings inside \fCtalk\fP
  1954. or \fCmail\fP, etc.
  1955. .PP
  1956. Some people like to bind \fC^S\fP and \fC^Q\fP to editor commands.
  1957. Just binding these has no effect, as the terminal will catch them and
  1958. use them for flow control.  You could unset them as stop and start
  1959. characters, but most people like to use these for external commands.
  1960. The solution is to set the \fINOFLOWCONTROL\fP option.  This will
  1961. allow you to bind the start and stop characters to editor commands,
  1962. while retaining their normal use for external commands.
  1963. .Sh "Parameter Substitution"
  1964. .PP
  1965. In \fBzsh\fP, parameters are set like this:
  1966. .Ds
  1967. %\0foo=bar
  1968. %\0echo\0$foo
  1969. bar
  1970. .De
  1971. Spaces before or after the \fC=\fP are frowned upon:
  1972. .Ds
  1973. %\0foo\0=\0bar
  1974. zsh:\0command\0not\0found:\0foo
  1975. .De
  1976. Also, \fCset\fP doesn't work for setting parameters:
  1977. .Ds
  1978. %\0set\0foo=bar
  1979. %\0set\0foo\0=\0bar
  1980. %\0echo\0$foo
  1981.  
  1982. %
  1983. .De
  1984. Note that no error message was printed.  This is because both
  1985. of these commands were perfectly valid; the \fCset\fP builtin
  1986. assigns its arguments to the \fIpositional parameters\fP
  1987. (\fC$1\fP, \fC$2\fP, etc.).
  1988. .Ds
  1989. %\0set\0foo=bar
  1990. %\0echo\0$1
  1991. foo=bar
  1992. %\0set\0foo\0=\0bar
  1993. %\0echo\0$3\0$2
  1994. bar\0=
  1995. .De
  1996. If you're really intent on using the csh syntax, define a
  1997. function like this:
  1998. .Ds
  1999. %\0set\0()\0{
  2000. >\0\0\0\0eval\0"$1$2$3"
  2001. >\0}
  2002. %\0set\0foo\0=\0bar
  2003. %\0set\0fuu=brrr
  2004. %\0echo\0$foo\0$fuu
  2005. bar\0brrr
  2006. .De
  2007. But then, of course you can't use the form of \fCset\fP with
  2008. options, like \fCset -F\fP (which turns off filename generation).
  2009. Also, the \fCset\fP command by itself won't list all the parameters
  2010. like it should.
  2011. To get around that you need a \fCcase\fP statement:
  2012. .Ds
  2013. %\0set\0()\0{
  2014. >\0\0\0\0case\0$1\0in
  2015. >\0\0\0\0-*|+*|'')\0builtin\0set\0$*\0;;
  2016. >\0\0\0\0*)\0eval\0"$1$2$3"\0;;
  2017. >\0\0\0\0esac
  2018. >\0}
  2019. .De
  2020. For the most part, this should make csh users happy.
  2021. .PP
  2022. The following sh-style operators are supported in \fBzsh\fP:
  2023. .Ds
  2024. %\0unset\0null
  2025. %\0echo\0${foo-xxx}
  2026. bar
  2027. %\0echo\0${null-xxx}
  2028. xxx
  2029. %\0unset\0null
  2030. %\0echo\0${null=xxx}
  2031. xxx
  2032. %\0echo\0$null
  2033. xxx
  2034. %\0echo\0${foo=xxx}
  2035. bar
  2036. %\0echo\0$foo
  2037. bar
  2038. %\0unset\0null
  2039. %\0echo\0${null+set}
  2040.  
  2041. %\0echo\0${foo+set}
  2042. set
  2043. .De
  2044. Also, csh-style \fC:\fP modifiers may be appended to a parameter
  2045. substitution.
  2046. .Ds
  2047. %\0echo\0$PWD
  2048. /home/learning/pf/zsh/zsh2.00/src
  2049. %\0echo\0$PWD:h
  2050. /home/learning/pf/zsh/zsh2.00
  2051. %\0echo\0$PWD:h:h
  2052. /home/learning/pf/zsh
  2053. %\0echo\0$PWD:t
  2054. src
  2055. %\0name=foo.c
  2056. %\0echo\0$name
  2057. foo.c
  2058. %\0echo\0$name:r
  2059. foo
  2060. %\0echo\0$name:e
  2061. c
  2062. .De
  2063. The equivalent constructs in ksh (which are also supported in \fBzsh\fP)
  2064. are a bit more general and easier to remember.
  2065. When the shell expands \fC${foo#\fR\fIpat\fR\fC}\fR,
  2066. it checks to see if \fIpat\fP matches a substring at the beginning
  2067. of the value
  2068. of \fCfoo\fP.  If so, it removes that portion of \fCfoo\fP, using the shortest
  2069. possible match.
  2070. With \fC${foo##\fR\fIpat\fR\fC}\fR, the longest possible match is removed.
  2071. \fC${foo%\fR\fIpat\fR\fC}\fR and \fC${foo%%\fR\fIpat\fR\fC}\fR remove the match
  2072. from the end.
  2073. Here are the ksh equivalents of the \fC:\fP modifiers:
  2074. .Ds
  2075. %\0echo\0${PWD%/*}
  2076. /home/learning/pf/zsh/zsh2.00
  2077. %\0echo\0${PWD%/*/*}
  2078. /home/learning/pf/zsh
  2079. %\0echo\0${PWD##*/}
  2080. src
  2081. %\0echo\0${name%.*}
  2082. foo
  2083. %\0echo\0${name#*.}
  2084. c
  2085. .De
  2086. \fBzsh\fP also has upper/lowercase modifiers:
  2087. .Ds
  2088. %\0xx=Test
  2089. %\0echo\0$xx:u
  2090. TEST
  2091. %\0echo\0$xx:l
  2092. test
  2093. .De
  2094. and a substitution modifier:
  2095. .Ds
  2096. %\0echo\0$name:s/foo/bar/
  2097. bar.c
  2098. %\0ls
  2099. foo.c\0\0\0\0foo.h\0\0\0\0foo.o\0\0\0\0foo.pro
  2100. %\0for\0i\0in\0foo.*;\0mv\0$i\0$i:s/foo/bar/
  2101. %\0ls
  2102. bar.c\0\0\0\0bar.h\0\0\0\0bar.o\0\0\0\0bar.pro
  2103. .De
  2104. There is yet another syntax to modify substituted parameters.  You can
  2105. add certain modifiers in parentheses after the opening brace like:
  2106. .Ds
  2107. ${(\fImodifiers\fC)\fIparameter\fC}
  2108. .De
  2109. For example, \fCo\fP sorts the words resulting from the expansion:
  2110. .Ds
  2111. %\0echo\0${path}
  2112. /usr/bin\0/usr/bin/X11\0/etc
  2113. %\0echo\0${(o)path}
  2114. /etc\0/usr/bin\0/usr/bin/X11
  2115. .De
  2116. One possible source of confusion is the fact that in \fBzsh\fP,
  2117. the result of parameter substitution is \fInot\fP split into
  2118. words.  Thus, this will not work:
  2119. .Ds
  2120. %\0srcs='glob.c\0exec.c\0init.c'
  2121. %\0ls\0$srcs
  2122. glob.c\0exec.c\0init.c\0not\0found
  2123. .De
  2124. This is considered a feature, not a bug.
  2125. If splitting were done by default, as it is in most other shells,
  2126. functions like this would not work properly:
  2127. .Ds
  2128. $\0ll\0()\0{\0ls\0-F\0$*\0}
  2129. $\0ll\0'fuu\0bar'
  2130. fuu\0not\0found
  2131. bar\0not\0found
  2132.  
  2133. %\0ll\0'fuu\0bar'
  2134. fuu\0bar\0not\0found
  2135. .De
  2136. Of course, a hackish workaround is available in sh (and \fBzsh\fP):
  2137. .Ds
  2138. %\0setopt\0shwordsplit
  2139. %\0ll\0()\0{\0ls\0-F\0"$@"\0}
  2140. %\0ll\0'fuu\0bar'
  2141. fuu\0bar\0not\0found
  2142. .De
  2143. If you like the sh behaviour, \fBzsh\fP can accomodate you:
  2144. .Ds
  2145. %\0ls\0${=srcs}
  2146. exec.c\0\0glob.c\0\0init.c
  2147. %\0setopt\0shwordsplit
  2148. %\0ls\0$srcs
  2149. exec.c\0\0glob.c\0\0init.c
  2150. .De
  2151. Another way to get the \fC$srcs\fP trick to work is to use an array:
  2152. .Ds
  2153. %\0unset\0srcs
  2154. %\0srcs=(\0glob.c\0exec.c\0init.c\0)\0\0
  2155. %\0ls\0$srcs
  2156. exec.c\0\0glob.c\0\0init.c
  2157. .De
  2158. or an alias:
  2159. .Ds
  2160. %\0alias\0-g\0SRCS='exec.c\0glob.c\0init.c'
  2161. %\0ls\0SRCS
  2162. exec.c\0\0glob.c\0\0init.c
  2163. .De
  2164. Another option that modifies parameter expansion is
  2165. \fIRCEXPANDPARAM\fP:
  2166. .Ds
  2167. %\0echo\0foo/$srcs
  2168. foo/glob.c\0exec.c\0init.c
  2169. %\0setopt\0rcexpandparam
  2170. %\0echo\0foo/$srcs
  2171. foo/glob.c\0foo/exec.c\0foo/init.c
  2172. %\0echo\0foo/${^srcs}
  2173. foo/glob.c\0foo/exec.c\0foo/init.c
  2174. %\0echo\0foo/$^srcs
  2175. foo/glob.c\0foo/exec.c\0foo/init.c
  2176. .De
  2177. .Sh "Shell Parameters"
  2178. .PP
  2179. The shell has many predefined parameters that may be
  2180. accessed.  Here are some examples:
  2181. .Ds
  2182. %\0sleep\010\0&
  2183. [1]\03820
  2184. %\0echo\0$!
  2185. 3820
  2186. %\0set\0a\0b\0c
  2187. %\0echo\0$#
  2188. 3
  2189. %\0echo\0$ARGC
  2190. 3
  2191. %\0(\0exit\020\0)\0;\0echo\0$?
  2192. 20
  2193. %\0false;\0echo\0$status
  2194. 1
  2195. .De
  2196. (\fC$?\fP and \fC$status\fP are equivalent.)
  2197. .Ds
  2198. %\0echo\0$HOST\0$HOSTTYPE
  2199. dendrite\0sun4
  2200. %\0echo\0$UID\0$GID
  2201. 701\060
  2202. %\0cd\0/tmp
  2203. %\0cd\0/home
  2204. %\0echo\0$PWD\0$OLDPWD
  2205. /home\0/tmp
  2206. %\0ls\0$OLDPWD/.getwd\0
  2207. /tmp/.getwd
  2208. .De
  2209. \fC~+\fP and \fC~-\fP are short for \fC$PWD\fP and \fC$OLDPWD\fP, respectively.
  2210. .Ds
  2211. %\0ls\0~-/.getwd
  2212. /tmp/.getwd
  2213. %\0ls\0-d\0~+/learning
  2214. /home/learning
  2215. %\0echo\0$RANDOM
  2216. 4880
  2217. %\0echo\0$RANDOM
  2218. 11785
  2219. %\0echo\0$RANDOM
  2220. 2062
  2221. %\0echo\0$TTY
  2222. /dev/ttyp4
  2223. %\0echo\0$VERSION
  2224. zsh\0v2.00.03
  2225. %\0echo\0$USERNAME
  2226. pf
  2227. .De
  2228. .PP
  2229. The \fCcdpath\fP variable sets the search path for the \fCcd\fP command.
  2230. If you do not specify \fC.\fP somewhere in the path, it is assumed to
  2231. be the first component.
  2232. .Ds
  2233. %\0cdpath=(\0/usr\0~\0~/zsh\0)
  2234. %\0ls\0/usr
  2235. 5bin\0\0\0\0\0\0\0\0\0dict\0\0\0\0\0\0\0\0\0lang\0\0\0\0\0\0\0\0\0net\0\0\0\0\0\0\0\0\0\0sccs\0\0\0\0\0\0\0\0\0sys
  2236. 5include\0\0\0\0\0etc\0\0\0\0\0\0\0\0\0\0lector\0\0\0\0\0\0\0nserve\0\0\0\0\0\0\0services\0\0\0\0\0tmp
  2237. 5lib\0\0\0\0\0\0\0\0\0export\0\0\0\0\0\0\0lib\0\0\0\0\0\0\0\0\0\0oed\0\0\0\0\0\0\0\0\0\0share\0\0\0\0\0\0\0\0ucb
  2238. adm\0\0\0\0\0\0\0\0\0\0games\0\0\0\0\0\0\0\0local\0\0\0\0\0\0\0\0old\0\0\0\0\0\0\0\0\0\0skel\0\0\0\0\0\0\0\0\0ucbinclude
  2239. bin\0\0\0\0\0\0\0\0\0\0geac\0\0\0\0\0\0\0\0\0lost+found\0\0\0openwin\0\0\0\0\0\0spool\0\0\0\0\0\0\0\0ucblib
  2240. boot\0\0\0\0\0\0\0\0\0hosts\0\0\0\0\0\0\0\0macsyma_417\0\0pat\0\0\0\0\0\0\0\0\0\0src\0\0\0\0\0\0\0\0\0\0xpg2bin
  2241. demo\0\0\0\0\0\0\0\0\0include\0\0\0\0\0\0man\0\0\0\0\0\0\0\0\0\0princeton\0\0\0\0stand\0\0\0\0\0\0\0\0xpg2include
  2242. diag\0\0\0\0\0\0\0\0\0kvm\0\0\0\0\0\0\0\0\0\0mdec\0\0\0\0\0\0\0\0\0pub\0\0\0\0\0\0\0\0\0\0swap\0\0\0\0\0\0\0\0\0xpg2lib
  2243. %\0cd\0spool
  2244. /usr/spool
  2245. %\0cd\0bin
  2246. /usr/bin
  2247. %\0cd\0func
  2248. ~/func
  2249. %\0cd\0
  2250. %\0cd\0pub
  2251. %\0pwd
  2252. /u/pfalstad/pub
  2253. %\0ls\0-d\0/usr/pub
  2254. /usr/pub
  2255. .De
  2256. \fBPATH\fP and \fBpath\fP both set the search path for commands.
  2257. These two variables are equivalent, except that one is a string
  2258. and one is an array.  If the user modifies \fBPATH\fP, the shell
  2259. changes \fBpath\fP as well, and vice versa.
  2260. .Ds
  2261. %\0PATH=/bin:/usr/bin:/tmp:.
  2262. %\0echo\0$path
  2263. /bin\0/usr/bin\0/tmp\0.
  2264. %\0path=(\0/usr/bin\0.\0/usr/local/bin\0/usr/ucb\0)
  2265. %\0echo\0$PATH
  2266. /usr/bin:.:/usr/local/bin:/usr/ucb
  2267. .De
  2268. The same is true of \fBCDPATH\fP and \fBcdpath\fP:
  2269. .Ds
  2270. %\0echo\0$CDPATH
  2271. /usr:/u/pfalstad:/u/pfalstad/zsh
  2272. %\0CDPATH=/u/subbarao:/usr/src:/tmp
  2273. %\0echo\0$cdpath
  2274. /u/subbarao\0/usr/src\0/tmp
  2275. .De
  2276. In general, predefined parameters with names in all lowercase are
  2277. arrays; assignments to them take the form:
  2278. .Ds
  2279. \fIname\fR\fC=(\fR\0\fIelem\fR\0...\\0\fC)\fR
  2280. .De
  2281. Predefined parameters with names in all uppercase are strings.  If
  2282. there is both an array and a string version of the same parameter, the
  2283. string version is a colon-separated list, like \fBPATH\fP.
  2284. .PP
  2285. \fBHISTFILE\fP is the name of the history file, where the history
  2286. is saved when a shell exits.
  2287. .Ds
  2288. %\0zsh
  2289. phoenix%\0HISTFILE=/tmp/history
  2290. phoenix%\0SAVEHIST=20
  2291. phoenix%\0echo\0foo
  2292. foo
  2293. phoenix%\0date
  2294. Fri\0May\024\005:39:35\0EDT\01991
  2295. phoenix%\0uptime
  2296. \0\05:39am\0\0up\04\0days,\020:02,\0\040\0users,\0\0load\0average:\02.30,\02.20,\02.00
  2297. phoenix%\0exit
  2298. %\0cat\0/tmp/history
  2299. HISTFILE=/tmp/history
  2300. SAVEHIST=20
  2301. echo\0foo
  2302. date
  2303. uptime
  2304. exit
  2305. %\0HISTSIZE=3
  2306. %\0history
  2307. \0\0\028\0\0rm\0/tmp/history
  2308. \0\0\029\0\0HISTSIZE=3
  2309. \0\0\030\0\0history
  2310. .De
  2311. If you have several incantations of \fBzsh\fP running at the same
  2312. time, like when using the X window system, it might be preferable to
  2313. append the history of each shell to a file when a shell exits instead
  2314. of overwriting the old contents of the file.  You can get this
  2315. behaviour by setting the \fIAPPENDHISTORY\fP option.
  2316. .PP
  2317. In \fBzsh\fP, if you say
  2318. .Ds
  2319. %\0>file
  2320. .De
  2321. the command \fCcat\fP is normally assumed:
  2322. .Ds
  2323. %\0>file
  2324. foo!\0\0\0\0
  2325. ^D
  2326. %\0cat\0file
  2327. foo!
  2328. .De
  2329. Thus, you can view a file simply by typing:
  2330. .Ds
  2331. %\0<file
  2332. foo!
  2333. .De
  2334. However, this is not csh or sh compatible.  To correct this,
  2335. change the value of the parameter \fBNULLCMD\fP,
  2336. which is \fCcat\fP by default.
  2337. .Ds
  2338. %\0NULLCMD=:
  2339. %\0>file
  2340. %\0ls\0-l\0file
  2341. -rw-r--r--\0\01\0pfalstad\0\0\0\0\0\0\0\00\0May\024\005:41\0file
  2342. .De
  2343. If \fCNULLCMD\fP is unset, the shell reports an error if no
  2344. command is specified (like csh).
  2345. .Ds
  2346. %\0unset\0NULLCMD
  2347. %\0>file
  2348. zsh:\0redirection\0with\0no\0command
  2349. .De
  2350. Actually, \fBREADNULLCMD\fP is used whenever you have a null command
  2351. reading input from a single file.  Thus, you can set \fBREADNULLCMD\fP
  2352. to \fCmore\fP or \fCless\fP rather than \fCcat\fP.  Also, if you
  2353. set \fBNULLCMD\fP to \fC:\fP for sh compatibility, you can still read
  2354. files with \fC< file\fP if you leave \fBREADNULLCMD\fP set to \fCmore\fP.
  2355. .Sh "Prompting"
  2356. .PP
  2357. The default prompt for \fBzsh\fP is:
  2358. .Ds
  2359. phoenix%\0echo\0$PROMPT
  2360. %m%#\0
  2361. .De
  2362. The \fC%m\fP stands for the short form of the current hostname,
  2363. and the \fC%#\fP stands for a \fC%\fP or a \fC#\fP, depending on whether
  2364. the shell is running as root or not.
  2365. \fBzsh\fP supports many other control sequences
  2366. in the \fBPROMPT\fP variable.
  2367. .Ds
  2368. %\0PROMPT='%/>\0'
  2369. /u/pfalstad/etc/TeX/zsh>
  2370.  
  2371. %\0PROMPT='%~>\0'\0\0\0
  2372. ~/etc/TeX/zsh>\0
  2373.  
  2374. %\0PROMPT='%h\0%~>\0'
  2375. 6\0~/etc/TeX/zsh>\0
  2376. .De
  2377. \fC%h\fP\0represents\0the\0number\0of\0current\0history\0event.
  2378. .Ds
  2379. %\0PROMPT='%h\0%~\0%M>\0'
  2380. 10\0~/etc/TeX/zsh\0apple-gunkies.gnu.ai.mit.edu>\0
  2381.  
  2382. %\0PROMPT='%h\0%~\0%m>\0'
  2383. 11\0~/etc/TeX/zsh\0apple-gunkies>\0
  2384.  
  2385. %\0PROMPT='%h\0%t>\0'
  2386. 12\06:11am>\0
  2387.  
  2388. %\0PROMPT='%n\0%w\0tty%l>'
  2389. pfalstad\0Fri\024\0ttyp0>
  2390. .De
  2391. \fBPROMPT2\fP is used in multiline commands, like for-loops.  The
  2392. \fC%_\fP escape sequence was made especially for this prompt.  It is
  2393. replaced by the kind of command that is being entered.
  2394. .Ds
  2395. %\0PROMPT2='%_>\0'
  2396. %\0for\0i\0in\0foo\0bar
  2397. for>
  2398.  
  2399. %\0echo\0'hi
  2400. quote>
  2401. .De
  2402. Also available is the \fBRPROMPT\fP parameter.
  2403. If this is set, the shell puts a prompt on the \fIright\fP side
  2404. of the screen.
  2405. .Ds
  2406. %\0RPROMPT='%t'
  2407. %\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\06:14am
  2408.  
  2409. %\0RPROMPT='%~'
  2410. %\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0~/etc/TeX/zsh
  2411.  
  2412. %\0PROMPT='%l\0%T\0%m[%h]\0'\0RPROMPT='\0%~'
  2413. p0\06:15\0phoenix[5]\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0~/etc/TeX/zsh
  2414. .De
  2415. These special escape sequences can also be used with the
  2416. \fC-P\fP option to \fCprint\fP:
  2417. .Ds
  2418. %\0print\0-P\0%h\0tty%l
  2419. 15\0ttyp1
  2420. .De
  2421. .PP
  2422. The \fBPOSTEDIT\fP parameter is printed whenever the editor exits.
  2423. This can be useful for termcap tricks.  To highlight the prompt
  2424. and command line while leaving command output unhighlighted, try this:
  2425. .Ds
  2426. %\0POSTEDIT=`echotc\0se`
  2427. %\0PROMPT='%S%%\0'
  2428. .De
  2429. .Sh "Login/logout watching"
  2430. .PP
  2431. You can specify login or logout events to monitor
  2432. by setting the \fBwatch\fP variable.
  2433. Normally, this is done by specifying a list of usernames.
  2434. .Ds
  2435. %\0watch=(\0pfalstad\0subbarao\0sukthnkr\0egsirer\0)
  2436. .De
  2437. The \fClog\fP command reports all people logged in
  2438. that you are watching for.
  2439. .Ds
  2440. %\0log
  2441. pfalstad\0has\0logged\0on\0p0\0from\0mickey.
  2442. pfalstad\0has\0logged\0on\0p5\0from\0mickey.
  2443. %\0\fR...\fC
  2444. subbarao\0has\0logged\0on\0p8\0from\0phoenix.
  2445. %\0\fR...\fC
  2446. subbarao\0has\0logged\0off\0p8\0from\0phoenix.
  2447. %\0\fR...\fC
  2448. sukthnkr\0has\0logged\0on\0p8\0from\0dew.
  2449. %\0\fR...\fC
  2450. sukthnkr\0has\0logged\0off\0p8\0from\0dew.
  2451. .De
  2452. If you specify hostnames with an \fC@\fP prepended,
  2453. the shell will watch for all users logging in from
  2454. the specified host.
  2455. .Ds
  2456. %\0watch=(\0@mickey\0@phoenix\0)
  2457. %\0log
  2458. djthongs\0has\0logged\0on\0q2\0from\0phoenix.
  2459. pfalstad\0has\0logged\0on\0p0\0from\0mickey.
  2460. pfalstad\0has\0logged\0on\0p5\0from\0mickey.
  2461. .De
  2462. If you give a tty name with a \fC%\fP prepended, the shell
  2463. will watch for all users logging in on that tty.
  2464. .Ds
  2465. %\0watch=(\0%ttyp0\0%console\0)
  2466. %\0log
  2467. root\0has\0logged\0on\0console\0from\0.
  2468. pfalstad\0has\0logged\0on\0p0\0from\0mickey.
  2469. .De
  2470. The format of the reports may also be changed.
  2471. .Ds
  2472. %\0watch=(\0pfalstad\0gettes\0eps\0djthongs\0jcorr\0bdavis\0)
  2473. %\0log
  2474. jcorr\0has\0logged\0on\0tf\0from\0128.112.176.3:0.
  2475. jcorr\0has\0logged\0on\0r0\0from\0128.112.176.3:0.
  2476. gettes\0has\0logged\0on\0p4\0from\0yo:0.0.
  2477. djthongs\0has\0logged\0on\0pe\0from\0grumpy:0.0.
  2478. djthongs\0has\0logged\0on\0q2\0from\0phoenix.
  2479. bdavis\0has\0logged\0on\0qd\0from\0BRUNO.
  2480. eps\0has\0logged\0on\0p3\0from\0csx30:0.0.
  2481. pfalstad\0has\0logged\0on\0p0\0from\0mickey.
  2482. pfalstad\0has\0logged\0on\0p5\0from\0mickey.
  2483. %\0WATCHFMT='%n\0on\0tty%l\0from\0%M'
  2484. %\0log
  2485. jcorr\0on\0ttytf\0from\0128.112.176.3:0.
  2486. jcorr\0on\0ttyr0\0from\0128.112.176.3:0.
  2487. gettes\0on\0ttyp4\0from\0yo:0.0
  2488. djthongs\0on\0ttype\0from\0grumpy:0.0
  2489. djthongs\0on\0ttyq2\0from\0phoenix.Princeto
  2490. bdavis\0on\0ttyqd\0from\0BRUNO.pppl.gov
  2491. eps\0on\0ttyp3\0from\0csx30:0.0
  2492. pfalstad\0on\0ttyp0\0from\0mickey.Princeton
  2493. pfalstad\0on\0ttyp5\0from\0mickey.Princeton
  2494. %\0WATCHFMT='%n\0fm\0%m'
  2495. %\0log
  2496. jcorr\0fm\0128.112.176.3:0
  2497. jcorr\0fm\0128.112.176.3:0
  2498. gettes\0fm\0yo:0.0
  2499. djthongs\0fm\0grumpy:0.0
  2500. djthongs\0fm\0phoenix
  2501. bdavis\0fm\0BRUNO
  2502. eps\0fm\0csx30:0.0
  2503. pfalstad\0fm\0mickey
  2504. pfalstad\0fm\0mickey
  2505. %\0WATCHFMT='%n\0%a\0at\0%t\0%w.'
  2506. %\0log
  2507. jcorr\0logged\0on\0at\03:15pm\0Mon\020.
  2508. jcorr\0logged\0on\0at\03:16pm\0Wed\022.
  2509. gettes\0logged\0on\0at\06:54pm\0Wed\022.
  2510. djthongs\0logged\0on\0at\07:19am\0Thu\023.
  2511. djthongs\0logged\0on\0at\07:20am\0Thu\023.
  2512. bdavis\0logged\0on\0at\012:40pm\0Thu\023.
  2513. eps\0logged\0on\0at\04:19pm\0Thu\023.
  2514. pfalstad\0logged\0on\0at\03:39am\0Fri\024.
  2515. pfalstad\0logged\0on\0at\03:42am\0Fri\024.
  2516. .De
  2517. If you have a \fC.friends\fP file in your home directory,
  2518. a convenient way to make \fBzsh\fP watch for all your friends
  2519. is to do this:
  2520. .Ds
  2521. %\0watch=(\0$(<\0~/.friends)\0)
  2522. %\0echo\0$watch
  2523. subbarao\0maruchck\0root\0sukthnkr\0\fR...
  2524. .De
  2525. If watch is set to \fCall\fP, then all users logging in or out
  2526. will be reported.
  2527. .Sh "Options"
  2528. .PP
  2529. Some options have already been mentioned; here are a few more:
  2530. .PP
  2531. Using the \fIAUTOCD\fP option, you can simply type the name
  2532. of a directory, and it will become the current directory.
  2533. .Ds
  2534. %\0cd\0/
  2535. %\0setopt\0autocd
  2536. %\0bin
  2537. %\0pwd
  2538. /bin
  2539. %\0../etc
  2540. %\0pwd
  2541. /etc
  2542. .De
  2543. With \fICDABLEVARS\fP, if the argument to \fCcd\fP is the name of a
  2544. parameter whose value is a valid directory, it will become
  2545. the current directory.
  2546. .Ds
  2547. %\0setopt\0cdablevars
  2548. %\0foo=/tmp
  2549. %\0cd\0foo
  2550. /tmp
  2551. .De
  2552. \fICORRECT\fP turns on spelling correction for commands,
  2553. and the \fICORRECTALL\fP option turns on spelling correction
  2554. for all arguments.
  2555. .Ds
  2556. %\0setopt\0correct
  2557. %\0sl
  2558. zsh:\0correct\0`sl'\0to\0`ls'\0[nyae]?\0y
  2559. %\0setopt\0correctall
  2560. %\0ls\0x.v11r4
  2561. zsh:\0correct\0`x.v11r4'\0to\0`X.V11R4'\0[nyae]?\0n
  2562. /usr/princton/src/x.v11r4\0not\0found
  2563. %\0ls\0/etc/paswd
  2564. zsh:\0correct\0to\0`/etc/paswd'\0to\0`/etc/passwd'\0[nyae]?\0y
  2565. /etc/passwd
  2566. .De
  2567. If you press \fCy\fP
  2568. when the shell asks you if you want to correct a word, it will
  2569. be corrected.  If you press \fCn\fP, it will be left alone.
  2570. Pressing \fCa\fP aborts the command, and pressing \fCe\fP brings the line
  2571. up for editing again, in case you agree the word is spelled wrong
  2572. but you don't like the correction.
  2573. .PP
  2574. Normally, a quoted expression may contain a newline:
  2575. .Ds
  2576. %\0echo\0'
  2577. >\0foo
  2578. >\0'
  2579.  
  2580. foo
  2581.  
  2582. %
  2583. .De
  2584. With \fICSHJUNKIEQUOTES\fP set, this is illegal, as it is
  2585. in csh.
  2586. .Ds
  2587. %\0setopt\0cshjunkiequotes
  2588. %\0ls\0'foo
  2589. zsh:\0unmatched\0'
  2590. .De
  2591. \fIGLOBDOTS\fP lets files beginning with a \fC.\fP be matched without
  2592. explicitly specifying the dot.
  2593. .Ds
  2594. %\0ls\0-d\0*x*
  2595. Mailboxes
  2596. %\0setopt\0globdots
  2597. %\0ls\0-d\0*x*
  2598. \&.exrc\0\0\0\0\0\0\0\0\0.pnewsexpert\0\0.xserverrc
  2599. \&.mushexpert\0\0\0.xinitrc\0\0\0\0\0\0Mailboxes
  2600. .De
  2601. \fIHISTIGNOREDUPS\fP prevents the current line from being
  2602. saved in the history if it is the same as the previous one;
  2603. \fIHISTIGNORESPACE\fP prevents the current line from being
  2604. saved if it begins with a space.
  2605. .Ds
  2606. %\0PROMPT='%h>\0'
  2607. 39>\0setopt\0histignoredups
  2608. 40>\0echo\0foo
  2609. foo
  2610. 41>\0echo\0foo
  2611. foo
  2612. 41>\0echo\0foo
  2613. foo
  2614. 41>\0echo\0bar
  2615. bar
  2616. 42>\0setopt\0histignorespace
  2617. 43>\0\0echo\0foo
  2618. foo
  2619. 43>\0\0echo\0fubar
  2620. fubar
  2621. 43>\0\0echo\0fubar
  2622. fubar
  2623. .De
  2624. \fIIGNOREBRACES\fP turns off csh-style brace expansion.
  2625. .Ds
  2626. %\0echo\0x{y{z,a},{b,c}d}e
  2627. xyze\0xyae\0xbde\0xcde
  2628. %\0setopt\0ignorebraces
  2629. %\0echo\0x{y{z,a},{b,c}d}e
  2630. x{y{z,a},{b,c}d}e
  2631. .De
  2632. \fIIGNOREEOF\fP forces the user to type \fCexit\fP or \fClogout\fP,
  2633. instead of just pressing \fI^D\fP.
  2634. .Ds
  2635. %\0setopt\0ignoreeof
  2636. %\0^D
  2637. zsh:\0use\0'exit'\0to\0exit.
  2638. .De
  2639. \fIINTERACTIVECOMMENTS\fP turns on interactive comments;
  2640. comments begin with a \fC#\fP.
  2641. .Ds
  2642. %\0setopt\0interactivecomments
  2643. %\0date\0#\0this\0is\0a\0comment
  2644. Fri\0May\024\006:54:14\0EDT\01991
  2645. .De
  2646. \fINOBEEP\fP makes sure the shell never beeps.
  2647. .PP
  2648. \fINOCLOBBER\fP prevents you from accidentally
  2649. overwriting an existing file.
  2650. .Ds
  2651. %\0setopt\0noclobber
  2652. %\0cat\0/dev/null\0>~/.zshrc
  2653. zsh:\0file\0exists:\0/u/pfalstad/.zshrc
  2654. .De
  2655. If you really do want to clobber a file, you can use the
  2656. \fC>!\fP operator.
  2657. To make things easier in this case, the \fC>\fP is stored in
  2658. the history list as a \fC>!\fP:
  2659. .Ds
  2660. %\0cat\0/dev/null\0>!\0~/.zshrc
  2661. %\0cat\0/etc/motd\0>\0~/.zshrc
  2662. zsh:\0file\0exists:\0/u/pfalstad/.zshrc
  2663. %\0!!
  2664. cat\0/etc/motd\0>!\0~/.zshrc
  2665. %\0\fR...
  2666. .De
  2667. \fIRCQUOTES\fP lets you use a more elegant method for including
  2668. single quotes in a singly quoted string:
  2669. .Ds
  2670. %\0echo\0'"don'\e''t\0do\0that."'
  2671. "don't\0do\0that."
  2672. %\0echo\0'"don''t\0do\0that."'
  2673. "dont\0do\0that."
  2674. %\0setopt\0rcquotes
  2675. %\0echo\0'"don''t\0do\0that."'
  2676. "don't\0do\0that."
  2677. .De
  2678. Finally,
  2679. \fISUNKEYBOARDHACK\fP wins the award for the strangest option.
  2680. If a line ends with \fC`\fP, and there are an odd number of them
  2681. on the line, the shell will ignore the trailing \fC`\fP.  This
  2682. is provided for keyboards whose RETURN key is too small,
  2683. and too close to the \fC`\fP key.
  2684. .Ds
  2685. %\0setopt\0sunkeyboardhack
  2686. %\0date`
  2687. Fri\0May\024\006:55:38\0EDT\01991
  2688. .De
  2689. .Sh "Closing Comments"
  2690. .PP
  2691. I (Bas de Bakker) would be happy to receive mail if anyone has any
  2692. tricks or ideas to add to this document, or if there are some points
  2693. that could be made clearer or covered more thoroughly.  Please notify
  2694. me of any errors in this document.
  2695. .if o \{\
  2696. .bp
  2697. .sv 1i
  2698. .\}
  2699. .pn 1
  2700. .bp
  2701. .PX
  2702.